Documente Academic
Documente Profesional
Documente Cultură
Table of Contents
CHAPTER 1 SQL AGGREGATE FUNCTIONS ........................................................................................... 2 AVG() Function .................................................................................................................................... 2 COUNT () Function .............................................................................................................................. 3 FIRST() Function .................................................................................................................................. 4 LAST() Function ................................................................................................................................... 5 MAX() Function ................................................................................................................................... 5 MIN() Function .................................................................................................................................... 6 SUM() Function ................................................................................................................................... 6 COMBINE AGGREGATE FUNCTIONS.................................................................................................... 7 CHAPTER 2 GROUP BY WITH HAVING CLAUSE .................................................................................... 8 GROUP BY ........................................................................................................................................... 8 HAVING ............................................................................................................................................... 9 CHAPTER 3 SET OPERATIONS ............................................................................................................. 11 UNION ALL......................................................................................................................................... 12 UNION ............................................................................................................................................... 12 INTERSECT ......................................................................................................................................... 13 MINUS ............................................................................................................................................... 13 RULES AND RESTRICTIONS ON SET OPERATIONS ............................................................................. 14 CHAPTER 4 SET THEORY AND SQL JOINS ........................................................................................... 16 SET THEORY ....................................................................................................................................... 16 SQL INNER JOIN................................................................................................................................. 18 SQL LEFT JOIN.................................................................................................................................... 18 SQL RIGHT JOIN ................................................................................................................................. 19 SQL FULL JOIN ................................................................................................................................... 19 SELF JOIN ........................................................................................................................................... 20 CROSS JOIN (CARTESIAN JON)........................................................................................................... 21 SQL INDEXES...................................................................................................................................... 22
AVG() Function
The AVG() function returns the average value of a numeric column. Syntax: SELECT AVG(column_name) FROM table_name Demo Database - "Products" table: ProductID 1001 1002 1003 1004 1005 1006 1007 1008 ProductName BirdEye Veg BirdEye Pie Tiger Beer A&W Root Beer F&N Ginger Beer F&N Orange Chef Anton's Gumbo Mix Chef Anton's Seasoning SupplierID 121200 121200 141401 131215 181628 181628 235811 235811 CategoryID 1 1 2 2 2 2 3 3 Unit 10 boxes x 10 bags 6 24 750m bottles 12 - 750m bottles 12 - 350m bottles 12 - 750m bottles 48 boxes 60 6 oz jar Price 25 3 48 24 13 12 96 34
Using the same Demo Database above the following SQL statement selects the "ProductName" and "Price" records that have an above average price: Query : SELECT ProductName, Price FROM Products WHERE Price>(SELECT AVG(Price) FROM Products); Result: ProductName Tiger Beer Chef Anton's Gumbo Mix Chef Anton's Seasoning Price 48 96 34 2
Ex- 1 The following SQL statement counts the total number of orders in the "Orders" table: SELECT COUNT(*) AS NumberOfOrders FROM Orders; Result: NumberOfOrders 8
Ex 2 COUNT(column_name) ExampleUsing the same Demo Database above the following SQL statement counts the number of orders from "EmployeeID"=5000 from the "Orders" table: SELECT COUNT(CustomerID) AS OrdFromCust FROM Orders WHERE EmpoyeeID=5000; Result: OrdFromCust 2 Ex 3 COUNT(DISTINCT column_name)
FIRST() Function
The FIRST() function returns the first value of the selected column. Syntax: SELECT FIRST(column_name) FROM table_name; Note: The FIRST() function is only supported in MS Access Workaround in SQL Server, MySQL and Oracle SQL Server Syntax SELECT TOP 1 column_name FROM table_name ORDER BY column_name ASC; MySQL Syntax SELECT column_name FROM table_name ORDER BY column_name ASC LIMIT 1; Oracle Syntax SELECT column_name FROM table_name ORDER BY column_name ASC WHERE ROWNUM <=1; Ex 1 Using the same Demo Database above the following SQL statement selects the first value of the "CustomerID" column from the "Orders" table: SELECT FIRST(CustomerID) AS FirstCustomer FROM Orders; Result: FirstCustomer 111001
MAX() Function
The MAX() function returns the largest value of the selected column. Syntax: SELECT MAX(column_name) FROM table_name; Demo Database - "Products" table: ProductID 1001 1002 1003 1004 1005 1006 1007 1008 ProductName BirdEye Veg BirdEye Pie Tiger Beer A&W Root Beer F&N Ginger Beer F&N Orange Chef Anton's Gumbo Mix Chef Anton's Seasoning SupplierID 121200 121200 141401 131215 181628 181628 235811 235811 CategoryID 1 1 2 2 2 2 3 3 Unit 10 boxes x 10 bags 6 24 750m bottles 12 - 750m bottles 12 - 350m bottles 12 - 750m bottles 48 boxes 60 6 oz jar Price 25 3 48 24 13 12 96 34
Ex 1 The following SQL statement gets the largest value of the "Price" column from the "Products" table: SELECT MAX(Price) AS HighestPrice FROM Products; Result: HighestPrice 96
SUM() Function
The SUM() function returns the total sum of a numeric column. Syntax: SELECT SUM(column_name) FROM table_name; Demo Database - "OrderDetails" table: OrderDetailID OrderID 1 10248 2 3 4 5 6 7 8 9 10 10248 10248 10249 10249 10250 10250 10250 10251 10251 ProductName BirdEye Veg BirdEye Pie Tiger Beer A&W Root Beer F&N Ginger Beer F&N Orange Chef Anton's Gumbo Mix Chef Anton's Seasoning BirdEye Chips BirdEye Peas Quantity 36 25 36 25 180 120 36 12 24 60
Ex 1 The following SQL statement finds the sum of all the "Quantity" fields for the "OrderDetails" table: SELECT SUM(Quantity) AS TotalQtyOrdered FROM OrderDetails; Result: TotalQtyOrdered 624
And a selection from the "Shippers" table: ShipperID ShipperName 1 2 3 Speedy Express United Package Phone (503) 555-9831 (503) 555-3199
And a selection from the "Employees" table: EmployeeID LastName FirstName 1 2 3 Davolio Fuller Leverling Nancy Andrew Janet BirthDate Photo Notes Education includes a BA
1968-12-08 EmpID1.pic
1952-02-19 EmpID2.pic Andrew received his BTS 1963-08-30 EmpID3.pic Janet has a BS degree
HAVING
The HAVING clause has been added to SQL because the WHERE keyword could not be used with aggregate functions. Syntax SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value; Ex- 3 Below is a selection from the "Orders" table: OrderID CustomerID EmployeeID 10248 10249 10250 90 81 34 5 6 4 OrderDate 1996-07-04 1996-07-05 1996-07-08 ShipperID 3 1 2
1968-12-08 EmpID1.pic
1952-02-19 EmpID2.pic Andrew received his BTS 1963-08-30 EmpID3.pic Janet has a BS degree
Now we want to find all the employees with more than 25 orders. SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders INNER JOIN Employees ON Orders.EmployeeID=Employees.EmployeeID) GROUP BY LastName HAVING COUNT(Orders.OrderID) > 25; Now we want to find if the employees "Davolio" or "Fuller" having more than 25 orders SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders INNER JOIN Employees ON Orders.EmployeeID=Employees.EmployeeID WHERE LastName='Davolio' OR LastName='Fuller' GROUP BY LastName HAVING COUNT(Orders.OrderID) > 25;
10
SQL statements containing these set operators are referred to as compound queries, and each SELECT statement in a compound query is referred to as a component query. Two SELECTs can be combined into a compound query by a set operation only if they satisfy the following two conditions: 1. The result sets of both the queries must have the same number of columns. 2. The datatype of each column in the second result set must match the datatype of its corresponding column in the first result set. 3. Minus and Intersect operators are not supported in all databases. E.g. MS Access
Set operations are often called vertical joins, because the result combines data from two or more SELECTS based on columns instead of rows. The generic syntax of a query involving a set operation is: <Component query> {UNION | UNION ALL | MINUS | INTERSECT} <Component query> Operator UNION ALL UNION Objective Combines the results of two SELECT statements into one result set. Combines the results of two SELECT statements into one result set, and then eliminates any duplicate rows from that result set. Takes the result set of one SELECT statement, and removes those rows that are also returned by a second SELECT statement. Returns only those rows that are returned by each of two SELECT statements.
MINUS
INTERSECT
Note: Minus and Intersect are supported only few databases like Oracle.
11
Query 2
SELECT C.CUST_NBR, C.NAME FROM CUSTOMER C WHERE C.CUST_NBR IN (SELECT O.CUST_NBR FROM CUST_ORDER O, EMPLOYEE E WHERE O.SALES_EMP_ID = E.EMP_ID AND E.LNAME = 'MARTIN');
Output: CUST_NBR NAME ---------- -----------------------------1 Cooper Industries 2 Emblazon Corp. 3 Ditech Corp. 4 Flowtech Inc. 5 Gentech Industries
If we look at the results returned by these two queries, we will notice that there is one common row (for Flowtech Inc.). Below discuss the effects of the various set operations between these two result sets.
UNION ALL
The UNION ALL operator merges the result sets of two component queries. This operation returns rows retrieved by either of the component queries. The following example illustrates the UNION ALL operation: Query:
SELECT CUST_NBR, NAME FROM CUSTOMER WHERE REGION_ID = 5 UNION ALL SELECT C.CUST_NBR, C.NAME FROM CUSTOMER C WHERE C.CUST_NBR IN (SELECT O.CUST_NBR FROM CUST_ORDER O, EMPLOYEE E WHERE O.SALES_EMP_ID = E.EMP_ID AND E.LNAME = 'MARTIN');
Output: CUST_NBR NAME ---------- -----------------------------1 Cooper Industries 2 Emblazon Corp. 3 Ditech Corp. 4 Flowtech Inc. 5 Gentech Industries 4 Flowtech Inc. 8 Zantech Inc.
As we can see from the result set, there is one customer, which is retrieved by both the SELECTs, and therefore appears twice in the result set. The UNION ALL operator simply merges the output of its component queries, without caring about any duplicates in the final result set.
UNION
The UNION operator returns all distinct rows retrieved by two component queries. The UNION operation eliminates duplicates while merging rows retrieved by either of the component queries. The following example illustrates the UNION operation:
12
CUST_NBR NAME ---------- -----------------------------1 Cooper Industries 2 Emblazon Corp. 3 Ditech Corp. 4 Flowtech Inc. 5 Gentech Industries 8 Zantech Inc.
INTERSECT
INTERSECT returns only the rows retrieved by both component queries. Compare this with UNION, which returns the rows retrieved by any of the component queries. If UNION acts like 'OR', INTERSECT acts like 'AND'. For example: Query:
SELECT CUST_NBR, NAME FROM CUSTOMER WHERE REGION_ID = 5 INTERSECT SELECT C.CUST_NBR, C.NAME FROM CUSTOMER C WHERE C.CUST_NBR IN (SELECT O.CUST_NBR FROM CUST_ORDER O, EMPLOYEE E WHERE O.SALES_EMP_ID = E.EMP_ID AND E.LNAME = 'MARTIN');
MINUS
MINUS returns all rows from the first SELECT that are not also returned by the second SELECT. For example: Query:
SELECT CUST_NBR, NAME FROM CUSTOMER WHERE REGION_ID = 5 MINUS SELECT C.CUST_NBR, C.NAME FROM CUSTOMER C WHERE C.CUST_NBR IN (SELECT O.CUST_NBR FROM CUST_ORDER O, EMPLOYEE E WHERE O.SALES_EMP_ID = E.EMP_ID AND E.LNAME = 'MARTIN');
Output: CUST_NBR NAME ---------- -----------------------------1 Cooper Industries 2 Emblazon Corp. 3 Ditech Corp. 5 Gentech Industries
An important thing to note here is that the execution order of component queries in a set operation is from top to bottom. The results of UNION, UNION ALL, and INTERSECT will not change if we alter the ordering of component queries. However, the result of MINUS will be different if we 13
The row for "Flowtech Inc." is returned by both queries, so in our first MINUS example the first component query adds "Flowtech Inc." to the result set while the second component query removes it. The second example turns the MINUS operation around. The first component query adds "Flowtech Inc." and "Zantech Inc." to the result set. The second component query specifies rows to subtract. One of the rows to subtract is "Flowtech Inc.", leaving "Zantech Inc." as the sole remaining row.
Output: Customer ID Customer Name ----------- ---------------------1 Cooper Industries 2 Emblazon Corp. 3 Ditech Corp. 4 Flowtech Inc. 5 Gentech Industries 8 Zantech Inc.
Although both SELECTs use column aliases, the result set takes the column names from the first SELECT. The same thing happens when we create a view based on a set operation. The column names in the view are taken from the first SELECT:
Rule-2 If we want to use ORDER BY in a query involving set operations, we must place the ORDER BY at the end of the entire statement.
14
Output: CUST_NBR NAME ---------- --------------------1 Cooper Industries 2 Emblazon Corp. 3 Ditech Corp. 4 Flowtech Inc. 5 Gentech Industries 6 MARTIN
15
But when you draw both the sets together, there are some common elements in both sets. Union of A and B: A B is the set of all elements that are in A, B or both. : A B = {1,2,3,4} U 1 A 2 3 4 B
A B = {1,2,3,4} Intersection of A and B: AB is the set of all elements that are in both A AND B: AB = {2,3} U 1 A 2 3 4 B
A B = {2,3} Complement: Complement of A is the set of all elements that are not in A but do exist in Universal set U. It is denoted as A 16
Symmetric difference: (A B) (AB) = A B is the set of elements that it is in either one of the sets but not both. (A B) (AB) = {1,4}
How this set theory is related to SQL joins? Lets find out now. Table1 EMPLOYEE_NAME EMP _ID EMP01 EMP02 EMP03 EMP04 EMP05 EMP06 EMP_NAME JAMES GERARD SARAH SANDY GARY JULIAN Table 2 EMPLOYEE_ROLE EMP _ID EMP01 EMP03 EMP05 EMP06 EMP01 EMP08 EMP_ROLE DEVELOPER ANALYST ADMIN TESTER CONFIG MGR COORDINATOR
Assumption: There is no referential integrity constrains between these two tables. Try to join these two tables in meaningful way with help of SQL joins.
17
Query:
SELECT EN.EMP_NAME, ER.EMP_ROLE FROM EMPLOYEE_NAME EN INNER JOIN EMPLOYEE_ROLE ER ON EN.EMP_ID = ER.EMP_ID
Output: EN.EMP_NAME JAMES JAMES SARAH GARY JULIAN ER.EMP_ROLE DEVELOPER CONFIG MGR ANALYST ADMIN TESTER
18
Output: EN.EMP_NAME JAMES JAMES GERARD SARAH SANDY GARY JULIAN ER.EMP_ROLE DEVELOPER CONFIG MGR <NULL> ANALYST <NULL> ADMIN TESTER
Query:
SELECT EN.EMP_NAME, ER.EMP_ROLE FROM EMPLOYEE_NAME EN RIGHT JOIN EMPLOYEE_ROLE ER ON EN.EMP_ID = ER.EMP_ID
Output EN.EMP_NAME JAMES JAMES SARAH GARY JULIAN <NULL> ER.EMP_ROLE DEVELOPER CONFIG MGR ANALYST ADMIN TESTER COORDINATOR
The SQL FULL JOIN combines the results of both left and right outer joins. The joined table will contain all records from both tables, and fill in NULLs for missing matches on either side. All rows in all joined tables and queries are included, whether they match or not. Note: Many databases like MySQL or MS Access do not support FULL OUTER JOIN. 19
Query:
SELECT EN.EMP_NAME, ER.EMP_ROLE FROM EMPLOYEE_NAME EN FULL OUTER JOIN EMPLOYEE_ROLE ER ON EN.EMP_ID = ER.EMP_ID
Output: EN.EMP_NAME JAMES JAMES GERARD SARAH SANDY GARY JULIAN <NULL> ER.EMP_ROLE DEVELOPER CONFIG MGR <NULL> ANALYST <NULL> ADMIN TESTER COORDINATOR
SELF JOIN
A self-join is a query in which a table is joined (compared) to itself. Self-joins are used to compare values in a column with other values in the same column in the same table. One practical use for self-joins: obtaining running counts and running totals in an SQL query. To list a table two times in the same query, you must provide a table alias for at least one of instance of the table name. This table alias helps the query processor determine whether columns should present data from the right or left version of the table. To understand the self join consider below table - EMPLOYEE_NAME EMP_ID EMP06 EMP02 EMP03 EMP04 EMP05 EMP01 Query
SELECT EN1.EMP_NAME AS Emp_Name , EN1.EMP_ID, EN2.EMP_NAME AS Report_To_NAME, EN2.EMP_ID AS REPORT_TO_ID FROM EMPLOYEE_NAME EN1, EMPLOYEE_NAME EN2 WHERE EN1.REPORT_TO_ID = EN2.EMP_ID
EMP_NAME REPORT_TO_ID JULIAN EMP01 GERARD EMP02 SARAH EMP02 SANDY EMP02 GARY EMP02 JAMES EMP02
20
Output EMP_NAME GERARD SARAH SANDY GARY JAMES JULIAN EMP_ID EMP02 EMP03 EMP04 EMP05 EMP01 EMP06 REPORT_TO_NAME GERARD GERARD GERARD GERARD GERARD JAMES REPORT_TO_ID EMP02 EMP02 EMP02 EMP02 EMP02 EMP01
Output NAME GERARD SARAH SANDY GARY JULIAN JAMES GERARD SARAH SANDY ROLE ANALYST ANALYST ANALYST ANALYST ANALYST ANALYST ADMIN ADMIN ADMIN
(Note: This table has 27 more rows which are not shown here)
So by any means if you have to use cross join or Cartesian join , limit your results by adding whee clause in query.
21
Query SELECT * FROM EMPLOYEE_NAME, EMPLOYEE_ROLE WHERE EMPLOYEE_ROLE.EMP_ROLE = 'DEVELOPER' Output EMP_ID EMP02 EMP03 EMP04 EMP05 EMP06 EMP01 EMP_NAME GERARD SARAH SANDY GARY JULIAN JAMES REPORT_TO_ID EMP02 EMP02 EMP02 EMP02 EMP01 EMP02 EMP_ID_1 EMP01 EMP01 EMP01 EMP01 EMP01 EMP01 EMP_ROLE DEVELOPER DEVELOPER DEVELOPER DEVELOPER DEVELOPER DEVELOPER
SQL INDEXES
An index can be created in the table to find data more quickly and efficiently. The users cant see indexes. They are used to speed up your search and execute query efficiently. Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So you should only create indexes on columns (and tables) that will be frequently searched against. Index can be created by using single or group of columns in a table. When index is created, it is assigned a ROWID for each row before it sorts out the data. Proper indexes are good for performance in large databases, but you need to be careful while creating index. Selection of fields depends on what you are using in your SQL queries. Syntax to create index on column
CREATE INDEX index_name ON table_name ( column1, column2.....);
22