SQL must know the second part

Lesson Seven

        This lesson explains what calculated fields are, how to create calculated fields, and how to use aliases to reference them from within your application

        The main code is as follows:

select vend_name + '(' + vend_country + ')'
from Vendors
order by vend_name;
select rtrim(vend_name) + '(' + rtrim(vend_country) + ')'
from Vendors
order by vend_name;
select rtrim(vend_name) + '(' + rtrim(vend_country) + ')'
	   as vend_title
from Vendors
order by vend_name;
select prod_id, quantity, item_price
from OrderItems
where order_num = 20008;
select prod_id, quantity, item_price, quantity*item_price as expanded_price
from OrderItems
where order_num = 20008;

        1. Functions to remove spaces include RTRIM, LTRIM and TRIM

        2. It is not recommended to use strings as alias names, if you do, enclose them in quotation marks

Summary: This lesson introduces calculated fields and how to create calculated fields. The use of computed fields in string concatenation and arithmetic computations is illustrated with examples. also. It also describes how to create and use aliases so that applications can reference calculated fields.


Lesson Eight

        This lesson explains what functions are, what functions are supported by the DBMS, and how to use them; it also explains why the use of SQL functions can be problematic

        The main code is as follows:

select vend_name, upper(vend_name) as vend_name_upcase
from Vendors
order by vend_name;
select cust_name, cust_contact
from Customers
where cust_contact = 'michael green';
select cust_name, cust_contact
from Customers
where soundex(cust_contact) = soundex('michael green');
select order_num
from Orders
where datepart(yy, order_date) = 2012;

        1. Since different DBMSs have specific functions, using functions will make the program less portable. If you want to use functions, you should make code comments

Summary: This lesson introduces how to use SQL's data manipulation functions. While these functions are very useful in formatting, manipulating, and filtering data, they are quite inconsistent across various SQL implementations


Lesson 9

        This lesson introduces what SQL aggregate functions are and how to use them to summarize table data

        The main code is as follows:

select avg(prod_price) as avg_price
from Products;
select avg(prod_price) as avg_price
from Products
where vend_id = 'DLL01';
select count(*) as num_count
from Customers;
select count(cust_email) as num_cust
from Customers;
select max(prod_price) as max_price
from Products;
select min(prod_price) as min_price
from Products;
select sum(quantity) as items_ordered
from OrderItems
where order_num = 20005;
select sum(item_price*quantity) as total_price
from OrderItems
where order_num = 20005;
select avg(distinct prod_price) as avg_price
from Products
where vend_id = 'DLL01';
select count(*) as num_items,
	   min(prod_price) as min_price,
	   max(prod_price) as max_price,
	   avg(prod_price) as avg_price
from products;

        1. Aggregate functions are functions that operate on certain rows, compute and return a value    

        2. AVG can only be used to determine the average value of a specific numeric column, and the column name must be given as a function parameter. The AVG function ignores rows with a NULL column value

        3. The COUNT function counts. If a column name is specified, the COUNT function will ignore rows with empty values ​​in the specified column. If an asterisk * is used in the COUNT function, it will not be ignored.

        4. Both the MAX and MIN functions ignore rows whose column value is NULL. Generally, these two functions are not used to return text columns.

        5. SUM is used to return the sum of the specified column values. The SUM function ignores rows with NULL column values.

        6. DISTINCT cannot be used for COUNT(*), DISTINCT must use column names

        7. When specifying an alias to contain the result of an aggregate function, the actual column name in the table should not be used

Summary: Aggregate functions are used to aggregate functions. SQL supports five aggregate functions, which can be used in a variety of ways to return the desired result. These functions are efficient, they return results that are generally much faster to compute in the client application


Lesson 10

        This lesson describes how to group data so that subsets of table contents can be summarized. This involves two new SELECT clauses: GROUP BY clause and HAVING clause

        The main code is as follows:

select count(*) as num_probs
from Products
where vend_id = 'DLL01';
select vend_id, count(*) as num_probs
from Products
group by vend_id;
select cust_id, count(*) as orders
from Orders
group by cust_id
having count(*) >= 2;
select vend_id, count(*) as num_probs
from Products
where prod_price >= 4
group by vend_id
having count(*) >= 2;
select vend_id, count(*) as num_probs
from Products
group by vend_id
having count(*) >= 2;
select order_num, count(*) as items
from OrderItems
group by order_num
having count(*) >= 3;
select order_num, count(*) as items
from OrderItems
group by order_num
having count(*) >= 3
order by items, order_num;

        1. The GROUP BY clause instructs the DBMS to group data and then aggregate on each group rather than the entire result set

        2. Before using the GROUP BY clause, there are some important rules         

  • The GROUP BY clause can contain any number of columns, so groups can be nested and grouped in more detail
  • If groupings are nested in the GROUP BY clause, the data will be aggregated on the last specified grouping. In other words, when creating groupings, all columns specified are calculated together (data cannot be retrieved from individual columns)
  • Each column listed in the GROUP BY clause must be a retrieval column or a valid expression (but not an aggregate function). If an expression is used in SELECT, the same expression must be specified in the GROUP BY clause. Aliases cannot be used
  • Most SQL implementations do not allow GROUP BY columns with variable-length data types (such as text or memo fields)
  • Except for aggregate calculation statements, every column in a SELECT statement must be given in the GROUP BY clause
  • If the grouping column contains rows with NULL values, then NULL will be returned as a grouping. If there are multiple rows of NULL values ​​in the column, they will be grouped together
  • The GROUP BY clause must appear after the WHERE clause and before the ORDER BY clause                   

        3. Some SQL implementations allow the GROUP BY clause to specify columns using relative positions, but are prone to errors

        4. HAVING filters groups, while WHERE filters rows

        5. WHERE is filtered before data grouping, and HAVING is filtered after data grouping. WHERE excluded rows are not included in the grouping, which may change the calculated value

        6. HAVING should be combined with the GROUP BY clause, while the WHERE clause is used for standard row-level filtering

        7. Difference between ORDER BY and GROUP BY   

  • ORDER BY sorts the resulting output, while GROUP BY groups the rows, but the output may not be in the grouped order
  • ORDER BY can be used on any column, GROUP BY can only use select columns or expression columns, and each select column expression must be used
  • ORDER BY is not necessarily required, GROUP BY must be used if a column (or expression) is used with aggregate functions

        8. Generally, when using the GROUP BY clause, the GROUP BY clause should also be given, which is the only way to ensure the correct ordering of the data

Summary: This lesson teaches how to use the GROUP BY clause to perform aggregate calculations on multiple groups of data, returning the results for each group. We saw how to use the HAVING clause to filter specific groups, and we also learned the difference between ORDER BY and GROUP BY and between WHERE and HAVING.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325795761&siteId=291194637