Sql query [select, sub] 4


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Sql query [select, sub] 4

  1. 1. <ul><li>DQL </li></ul><ul><li>[Data Query Language] </li></ul>
  2. 2. Syntax for SELECT statement: <ul><li>  </li></ul><ul><li>SELECT <COLUMN-LIST> FROM <TABLE-NAME> [WHERE <CONDITION>] GROUP BY <COLUMN-NAME(S)>] [HAVING <CONDITION>] [ORDER BY <EXPRESSION>]; </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>Example: Select * from EMP; </li></ul>
  3. 3. Selecting one or more columns from the table <ul><li>It is possible to select only few columns from the table. </li></ul><ul><li>To list employee number, employee name, job and salary from EMP table, the following query to be given. </li></ul><ul><li>  </li></ul><ul><li>Example: SELECT EMPNO, ENAME, SAL, JOB FROM EMP; </li></ul><ul><li>  </li></ul><ul><li>It is not necessary for the column(s) in the SELECT statement, to be in the same order of the table. </li></ul><ul><li>  </li></ul>
  4. 4. Changing Column names using Aliases <ul><li>To distinguish the real column name with a more meaning full name for better understanding while displaying the results then, an alias can be used after that column name. </li></ul><ul><li>In EMP table the column MGR stands for MANAGER. </li></ul><ul><li>To display MGR column as MANAGER the query can be written using alias as follows: </li></ul><ul><li>  </li></ul><ul><li>SQL> SELECT EMPNO, ENAME, MGR &quot;MANAGER&quot; FROM EMP; </li></ul>
  5. 5. WHERE Clause <ul><li>  Conditional retrieval of rows using WHERE clause </li></ul><ul><li>Example: SELECT * FROM EMP WHERE DEPTNO = 10; </li></ul><ul><li>WHERE clause filters the EMP table with DEPTNO = 10 alone. </li></ul><ul><li>Oracle checks the DEPTNO column in each row for an entry as 10, </li></ul><ul><li>if found then it sends them as output else skips the rows which does not match the condition. </li></ul><ul><li>The WHERE clause in the SELECT statement can have Relational, Logical, Arithmetic and String Operators. </li></ul><ul><li>Example 1: SELECT * FROM EMP WHERE ENAME = ‘SMITH’; </li></ul>
  6. 6. Working with NULL values <ul><li>  NULL values are not 0 or blank. </li></ul><ul><li>  It represents an unknown or inapplicable value </li></ul><ul><li>It cannot be compared using the relational and/or logical operators. </li></ul><ul><li>The special operator ‘IS’ is used with the keyword ‘NULL’ to locate NULL values. </li></ul><ul><li>Examples: </li></ul><ul><li>SELECT ENAME FROM EMP WHERE COMM IS NULL ; </li></ul><ul><li>  (The above command will list the employee(s) who is not getting commission). </li></ul><ul><li>   </li></ul>
  7. 7. Order by Clause <ul><li>Displaying the results in a sorted order </li></ul><ul><li>SQL uses the ORDER BY clause to impose an order on the result of a query. </li></ul><ul><li>ORDER BY clause is used with SELECT statement. </li></ul><ul><li>One or more columns and/or expressions can be specified in ORDER BY clause. </li></ul><ul><li>The ORDER BY clause sorts the query output according to the values in one or more selected columns. </li></ul><ul><li>Multiple columns are ordered one with another, and the user can specify whether to order them in ascending or descending order. </li></ul><ul><li>Ascending is default . </li></ul>
  8. 8. <ul><li>Syntax: SELECT <COLUMNS> FROM <TABLE-NAME> ORDER BY </li></ul><ul><li>[<COLUMN-NAME>, <COLUMN-NAME>] ASC/DESC; </li></ul><ul><li>Example 1: SELECT * FROM EMP ORDER BY ENAME; </li></ul><ul><li>(The above command will list all the employees from EMP table in ascending order of employee name) </li></ul><ul><li>Example 2: SELECT EMPNO, ENAME, DEPTNO, SAL FROM EMP ORDER BY DEPTNO; </li></ul><ul><li>(The above command will list all the employees from EMP table in ascending order of Department number) </li></ul><ul><li>Example 3: SELECT EMPNO, ENAME, HIREDATE EMP ORDER BY HIREDATE DESC; </li></ul><ul><li>(The above command will list all the employees from EMP table in descending of hiredate). </li></ul>
  9. 9. Sorting the result on multiple columns   <ul><li>To sort DEPTNO of EMP table in ascending order and then to further sort salary in each department in descending order, the following example query can be issued. </li></ul><ul><li>Example: </li></ul><ul><li>SELECT EMPNO, ENAME, DEPTNO, SAL </li></ul><ul><li>FROM EMP </li></ul><ul><li>ORDER BY DEPTNO, SAL DESC; </li></ul><ul><li>  </li></ul>
  10. 10. GROUP BY clause   <ul><li>ORDER BY acts upon rows whereas GROUP BY acts upon groups. </li></ul><ul><li>Example: </li></ul><ul><li>SELECT MAX(SAL), MIN(SAL), AVG(SAL), COUNT(*), </li></ul><ul><li>SUM(SAL) FROM EMP; </li></ul><ul><li>Output: </li></ul><ul><li>MAX(SAL) MIN(SAL) AVG(SAL) COUNT(*) SUM(SAL) </li></ul><ul><li>------------- -------------- ------------- ------------- ------------- </li></ul><ul><li>5000 800 2073.2143 14 29025 </li></ul><ul><li>  </li></ul>
  11. 11. <ul><li>To find the maximum of salary for department 10, then the query has to be: </li></ul><ul><li>  </li></ul><ul><li>SQL > SELECT MAX(SAL) FROM EMP WHERE DEPTNO = 10; </li></ul><ul><li>  </li></ul><ul><li>Output: MAX(SAL) </li></ul><ul><li>-------------- </li></ul><ul><li> 5000 </li></ul><ul><li>  </li></ul>
  12. 12. <ul><li>To find Department wise the sum of salary, max of salary, min salary etc., GROUP BY clause comes handy to group the records of the table based on a grouping column. </li></ul><ul><li>  GROUP BY clause is used with SELECT to combine a group of rows based on the values of a particular column or expression. </li></ul><ul><li>Aggregate functions are used to return summary information for each group. </li></ul><ul><li>The aggregate functions are applied to the individual groups. </li></ul><ul><li>By default, all the rows of a table are treated as a single group only. The following example will list the max of salary from each department. </li></ul>
  13. 13. <ul><li>Example: </li></ul><ul><li>SELECT MAX(SAL), DEPTNO FROM EMP GROUP BY DEPTNO; </li></ul><ul><li>  Output: MAX(SAL) DEPTNO </li></ul><ul><li>-------- --------- </li></ul><ul><li>5000 10 </li></ul><ul><li>3000 20 </li></ul><ul><li>2850  30 </li></ul>
  14. 14. <ul><li>The GROUP BY DEPTNO clause divides the rows of the table into groups based on their DEPTNO. </li></ul><ul><li>The group function MAX(SAL) is then applied to the rows in each group. </li></ul><ul><li>A query that uses the GROUP BY clause is termed as a ‘grouped query’. </li></ul><ul><li>The columns named in the GROUP BY clause are called the ‘ grouping columns’. </li></ul><ul><li>They determine how the rows should be grouped. </li></ul><ul><li>If the SQL statement does not contain a WHERE clause, place the GROUP BY clause after the FROM clause. </li></ul><ul><li>If it has a WHERE clause, place the GROUP BY clause after the WHERE clause. </li></ul>
  15. 15. <ul><li>The example lists the number of employees in each job. </li></ul><ul><li>Example: SELECT COUNT(*), JOB FROM EMP GROUP BY JOB; </li></ul><ul><li>  Output: COUNT(*) JOB </li></ul><ul><li>--------- --------- </li></ul><ul><li> 2 ANALYST </li></ul><ul><li> 4 CLERK </li></ul><ul><li> 3 MANAGER </li></ul><ul><li> 1 PRESIDENT </li></ul><ul><li>4                   SALESMAN </li></ul><ul><li>  </li></ul>
  16. 16. Use of WHERE clause and GROUP BY clause . <ul><li>Example: </li></ul><ul><li>SELECT COUNT(*), JOB FROM EMP WHERE DEPTNO = 10 GROUP BY JOB; </li></ul><ul><li>Output: COUNT(*) JOB </li></ul><ul><li>--------- --------- </li></ul><ul><li>1 CLERK </li></ul><ul><li>1 MANAGER </li></ul><ul><li>1 PRESIDENT </li></ul><ul><li>First the WHERE clause filters the rows based on the condition. </li></ul><ul><li>Followed by this, rows are grouped job-wise. </li></ul><ul><li>Then group function COUNT operates on the rows of each group to calculate group wise count. </li></ul>
  17. 17. Grouping rows based on multiple columns <ul><li>Grouping the rows of the table based on more than one column. </li></ul><ul><li>The following example lists the number of employees for each job type within department. ( Dept wise and then Designation wise ) </li></ul><ul><li>Example </li></ul><ul><li>SELECT DEPTNO, JOB, COUNT(*) FROM EMP GROUP BY DEPTNO, JOB; </li></ul><ul><li>Output: DEPTNO JOB COUNT(*) </li></ul><ul><li>--------- --------- --------- </li></ul><ul><li>10 CLERK 1 </li></ul><ul><li>10 MANAGER 1 </li></ul><ul><li>10 PRESIDENT 1 </li></ul><ul><li>20 ANALYST 2 </li></ul><ul><li>20 CLERK 2 </li></ul><ul><li>20 MANAGER 1 </li></ul><ul><li>30 CLERK 1 </li></ul><ul><li>30 MANAGER 1 </li></ul><ul><li>30 SALESMAN 4 </li></ul>
  18. 18. HAVING Clause <ul><li>The GROUP BY . . . HAVING clause is parallel to the WHERE clause. </li></ul><ul><li>WHERE clause acts upon rows whereas GROUP BY . . . HAVING act on groups. </li></ul><ul><li>As specific rows can be fetched with a WHERE clause, specific groups can be fetched using HAVING clause. </li></ul><ul><li>HAVING clause has to be placed after the GROUP BY clause when queried. </li></ul><ul><li>The HAVING clause works much like the WHERE clause, except that its logic is related to the results of group functions. </li></ul>
  19. 19. Example to illustrates the use of GROUP BY . . .HAVING clause: <ul><li>Select the dept having more than 3 employees </li></ul><ul><li>Example 1: </li></ul><ul><li>SELECT DEPTNO, COUNT(*) FROM EMP </li></ul><ul><li>GROUP BY DEPTNO HAVING COUNT(*) > 3; </li></ul><ul><li>  </li></ul><ul><li>Output: DEPTNO COUNT(*) </li></ul><ul><li>--------- --------- </li></ul><ul><li> 20 5 </li></ul><ul><li> 30 6 </li></ul><ul><li>    </li></ul>
  20. 20. <ul><li>To display the managers, who have more than 2 people reporting to them. </li></ul><ul><li>  </li></ul><ul><li>Example 2: </li></ul><ul><li>SELECT MGR, COUNT(*) FROM EMP </li></ul><ul><li> GROUP BY MGR </li></ul><ul><li>HAVING COUNT(*) > 2; </li></ul><ul><li>  </li></ul><ul><li>Output: MGR COUNT(*) </li></ul><ul><li>-------- -------- </li></ul><ul><li>7698 5 </li></ul><ul><li>7839   3 </li></ul>
  21. 21. <ul><li>The following example illustrates WHERE clause, GROUP BY … HAVING clause and ORDER BY clause. </li></ul><ul><li>  </li></ul><ul><li>Example: SELECT COUNT(*), SUM(SAL), JOB FROM EMP WHERE DEPTNO = 20 GROUP BY JOB HAVING SUM(SAL) > 2000 ORDER BY JOB; </li></ul><ul><li>  </li></ul><ul><li>Output: COUNT(*) SUM(SAL) JOB </li></ul><ul><li>------------ -------------- --------- </li></ul><ul><li>2 6000 ANALYST </li></ul><ul><li>1 2975 MANAGER </li></ul>
  22. 22. Difference between Group By, Having Clause and Order By, Where Clause: <ul><li>  </li></ul><ul><li>Group by clause groups the rows into smaller groups according to the given query and then shows the output in a particular sequence. </li></ul><ul><li>Having Clause is used for condition retrieval of rows from a grouped result. </li></ul><ul><li>Order by clause imposes an order on the result of a query. </li></ul><ul><li>Where Clause with Order by is used for conditional retrieval of individual rows. </li></ul>
  23. 23. <ul><li>SUB - QUERIES </li></ul><ul><li>  </li></ul><ul><li>Sub-Queries are nothing but nested SELECT statement. </li></ul><ul><li>The output of inner query is taken as the input of outer query. </li></ul><ul><li>Sub-queries are used in the WHERE or HAVING clause of another SQL statement (Parent/Main Query). </li></ul><ul><li>SQL first evaluates the inner query (or sub query) within the WHERE clause. </li></ul><ul><li>The inner query generates values that are tested in the predict of the outer query. </li></ul><ul><li>The return value of inner query is then substituted in the condition of the outer query. </li></ul><ul><li>Sub Queries can use commands namely SELECT, INSERT, UPDATE, DELETE and CREATE. </li></ul><ul><li>Sub query can itself contain a sub query. Oracle 8i places no limit on the level of query nesting. </li></ul>
  24. 24. <ul><li>Advantage of Sub-Queries </li></ul><ul><li>  </li></ul><ul><li>    The nested sub-query is very useful when you need to select rows from a table with a condition that depends on the data in the table itself. </li></ul><ul><li>  </li></ul>
  25. 25. Types of Sub-Queries <ul><li>  </li></ul><ul><li>Sub-query returning one row </li></ul><ul><li>Sub-query returning more than one row </li></ul><ul><li>Correlated Sub-query </li></ul>
  26. 26. Sub-query returning one row <ul><li>List the employees belonging to the department of ALLEN. </li></ul><ul><li>Example : </li></ul><ul><li>Step 1: SELECT DEPTNO FROM EMP </li></ul><ul><li>WHERE ENAME = 'ALLEN'; </li></ul><ul><li>  Output: DEPTNO </li></ul><ul><li>--------- </li></ul><ul><li> 30 </li></ul><ul><li>  Step 2: SELECT ENAME FROM EMP WHERE DEPTNO = 30; </li></ul><ul><li>  Output: ENAME </li></ul><ul><li>---------- </li></ul><ul><li>ALLEN </li></ul><ul><li>WARD </li></ul><ul><li>MARTIN </li></ul><ul><li>BLAKE </li></ul><ul><li>TURNER </li></ul><ul><li>JAMES </li></ul>
  27. 27. <ul><li>The two queries can be combined as a single query. </li></ul><ul><li>  </li></ul><ul><li>Example: </li></ul><ul><li>SELECT ENAME FROM EMP </li></ul><ul><li>WHERE DEPTNO = </li></ul><ul><li>(SELECT DEPTNO FROM EMP </li></ul><ul><li>WHERE ENAME = ‘ALLEN’); </li></ul><ul><li>Output: ENAME </li></ul><ul><li>---------- </li></ul><ul><li>ALLEN </li></ul><ul><li>WARD </li></ul><ul><li>MARTIN </li></ul><ul><li>BLAKE </li></ul><ul><li>TURNER </li></ul><ul><li>JAMES </li></ul><ul><li>  </li></ul>
  28. 28. <ul><li>Sub-query can be used to select the values from multiple tables. </li></ul><ul><li>Example: List the employee(s) who are working in SALES department. </li></ul><ul><li>SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP </li></ul><ul><li>WHERE DEPTNO = </li></ul><ul><li>(SELECT DEPTNO FROM DEPT </li></ul><ul><li>WHERE DNAME = ‘SALES’); </li></ul><ul><li>  </li></ul><ul><li>Output: EMPNO ENAME JOB DEPTNO </li></ul><ul><li>--------- ---------- --------- - -------- </li></ul><ul><li>7499 ALLEN SALESMAN 30 </li></ul><ul><li>7521 WARD SALESMAN 30 </li></ul><ul><li>7654 MARTIN SALESMAN 30 </li></ul><ul><li>7698 BLAKE MANAGER 30 </li></ul><ul><li>7844 TURNER SALESMAN 30 </li></ul><ul><li>7900 JAMES CLERK 30 </li></ul>
  29. 29. Sub-query returning more than one row <ul><li>  When a sub-query returns more than one row we need to use multi-row comparison operator. </li></ul><ul><li>List the employees who are acting as Managers . </li></ul><ul><li>Example: </li></ul><ul><li>SELECT EMPNO, ENAME FROM EMP </li></ul><ul><li>WHERE EMPNO IN </li></ul><ul><li>(SELECT MGR FROM EMP); </li></ul><ul><li>Output: EMPNO ENAME </li></ul><ul><li> ------- ---------- </li></ul><ul><li> 7566 JONES </li></ul><ul><li> 7698 BLAKE </li></ul><ul><li> 7782 CLARK </li></ul><ul><li> 7788 SCOTT </li></ul><ul><li>7839 KING </li></ul><ul><li> 7902 FORD </li></ul><ul><li>  </li></ul>
  30. 30. <ul><li>List the employees who are getting highest salary in each department from EMP table. </li></ul><ul><li>Example: </li></ul><ul><li>SELECT EMPNO, ENAME, SAL, DEPTNO FROM EMP </li></ul><ul><li>WHERE SAL </li></ul><ul><li>IN (SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO); </li></ul><ul><li>  Output : EMPNO ENAME SAL DEPTNO </li></ul><ul><li>---------- ---------- --------- --------- </li></ul><ul><ul><li> 7698 BLAKE 2850 30 </li></ul></ul><ul><li> 7788 SCOTT 3000 20 </li></ul><ul><li> 7902 FORD 3000 20 </li></ul><ul><li>7839 KING 5000 10 </li></ul>
  31. 31. <ul><li>List the employee(s) whose salary is more than all MANAGER’s Salary from EMP table. </li></ul><ul><li>  </li></ul><ul><li>Example: </li></ul><ul><li>SELECT EMPNO, ENAME, SAL FROM EMP </li></ul><ul><li>WHERE SAL > ALL (SELECT SAL FROM EMP </li></ul><ul><li>WHERE JOB = 'MANAGER'); </li></ul><ul><li>  </li></ul><ul><li>Output: EMPNO ENAME SAL </li></ul><ul><li>--------- ---------- --------- </li></ul><ul><li> 7788 SCOTT 3000 </li></ul><ul><li> 7839 KING 5000 </li></ul><ul><li> 7902 FORD 3000 </li></ul><ul><li>  </li></ul>
  32. 32. To note <ul><li>The inner query must be enclosed in parenthesis. </li></ul><ul><li>The inner query must be on the right hand side of the condition. </li></ul><ul><li>The sub-query may not have an order by clause. </li></ul><ul><li>The ORDER BY clause appears at the end of the main select statement. </li></ul><ul><li>Sub-queries are always executed from the most deeply nested to the least deeply nested, unless they are correlated sub-query. </li></ul>
  33. 33. Correlated Sub-query <ul><li>  A correlated sub-query is a nested sub-query. </li></ul><ul><li>The outer most query will be executed first. </li></ul><ul><li>The sub-query is executed repeatedly, once for each row of main(outer) query table. </li></ul><ul><li>In a correlated sub-query, the column value used in inner sub-query refers to the column value present in the outer query forming a correlated sub-query. </li></ul>
  34. 34. <ul><li>Example: </li></ul><ul><li>List the employees who earn salary greater than the average salary for their own department. </li></ul><ul><li>SELECT EMPNO, ENAME, SAL, DEPTNO FROM EMP A WHERE SAL >(SELECT AVG(SAL) FROM EMP WHERE DEPTNO = A.DEPTNO); </li></ul><ul><li>  Output: EMPNO ENAME SAL DEPTNO </li></ul><ul><li>---------- ---------- --------- --------- </li></ul><ul><li> 7499 ALLEN 1600 30 </li></ul><ul><li>7566 JONES 2975 20 </li></ul><ul><li> 7698 BLAKE 2850 30 </li></ul><ul><li>7788 SCOTT 3000 20 </li></ul><ul><li> 7839 KING 5000 10 </li></ul><ul><li>7902 FORD 3000 20 </li></ul><ul><li>  </li></ul>
  35. 35. Order of execution (corelated subquery) <ul><li>Outer query is executed first </li></ul><ul><li>select empno,ename,deptno,sal from emp; </li></ul><ul><li>Inner query is executed for each row in the outer query </li></ul><ul><li>select avg(sal) from emp where deptno=10; </li></ul><ul><li>and </li></ul><ul><li>Compares where the salary is greater for each employeed in deptno 10; </li></ul>
  36. 36. Using Special Operators in Sub-queries   <ul><li>EXISTS </li></ul><ul><li>ANY </li></ul><ul><li>SOME </li></ul><ul><li>ALL operators </li></ul>
  37. 37. Operators <ul><li> EXISTS </li></ul><ul><li>This operator is used to check the existence of values </li></ul><ul><li>This operator produces a Boolean result </li></ul><ul><li>It takes a sub-query as an argument and evaluates it to True if the sub-query produce any output and false, if the sub-query does not produce any output </li></ul><ul><li>     </li></ul><ul><li>ANY, SOME and ALL </li></ul><ul><li>Used along with the relational operators </li></ul><ul><li>Similar to IN operator, but only used in sub-queries </li></ul><ul><li>  </li></ul>
  38. 38. Example for EXISTS operator <ul><li>    List the Employee who has at least one person reporting to him. </li></ul><ul><li>Example 1: </li></ul><ul><li>SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP E </li></ul><ul><li>WHERE EXISTS </li></ul><ul><li>(SELECT EMPNO FROM EMP WHERE MGR = E.EMPNO); </li></ul><ul><li>  </li></ul><ul><li>Output: EMPNO ENAME SAL DEPTNO </li></ul><ul><li>-------- ------------ -------- --------- </li></ul><ul><li>7499 ALLEN 1600 30 </li></ul><ul><li>7566 JONES 2975 20 </li></ul><ul><li>7698 BLAKE 2850 30 </li></ul><ul><li>7788 SCOTT 3000 20 </li></ul><ul><li>7839 KING 5000 10 </li></ul><ul><li> 7902 FORD 3000 20 </li></ul>
  39. 39. Differences between Correlated Sub-queries and Non- Correlated Sub-queries:   <ul><li>In a non-correlated sub-query the inner most query is executed first. </li></ul><ul><li>But in a Correlated sub-query the outer most query is analyzed first and then based on its result the next-query is initiated </li></ul><ul><li>The sub-query is executed repeatedly, once for each row of main(outer) query table. </li></ul>