Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]

Similar presentations


Presentation on theme: "SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]"— Presentation transcript:

1 SQL- DQL (Oracle Version)

2 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list] [HAVING conditional expression] [ORDER BY column_list] ;

3 3 The SELECT Statement  SELECT - List the columns (and expressions) that should be returned from the query  FROM - Indicate the table(s) or view(s) from which data will be obtained  WHERE - Indicate the conditions under which a row will be included in the result  GROUP BY - Indicate categorization of results  HAVING - Indicate the conditions under which a category (group) will be included  ORDER BY - Sorts the result according to specified criteria

4 4 SELECTcolumn1, column2, column3,... FROM table; SELECTcolumn1, column2, column3,... FROM table; The Basic SELECT Statement  SELECT identifies what columns  FROM identifies which table

5 5 DEPTNO LOC --------- ------------- 10 NEW YORK 20 DALLAS 30 CHICAGO 40 BOSTON SQL> SELECT deptno, loc 2 FROM dept; Example SELECT Statement

6 6 DEPTNO DNAME LOC --------- -------------- ––––––––––––- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON SQL> SELECT * 2 FROM dept; Selecting All Columns

7 7 LOCATION ------------- NEW YORK DALLAS CHICAGO BOSTON SQL> SELECT loc AS location 2 FROM dept; Specifying Output Headings Note Upper-Case Heading

8 8 Location ------------- NEW YORK DALLAS CHICAGO BOSTON SQL> SELECT loc AS “Location” 2 FROM dept; Specifying Output Headings Note Mixed-Case HeadingDouble quotes

9 9 JOB --------- PRESIDENT MANAGER SALESMAN... SQL> SELECT job 2 FROM emp; Duplicate Output A total of 14 records display

10 10 JOB --------- ANALYST CLERK MANAGER PRESIDENT SALESMAN SQL> SELECT DISTINCT job 2 FROM emp; Suppressing Duplicate Output Each unique job is listed only once DISTINCT precedes the list of fields

11 11 ENAME JOB SAL ---------- --------- --------- JAMES CLERK 950 SMITH CLERK 800 ADAMS CLERK 1100 MILLER CLERK 1300 SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE job = ‘CLERK’; Limiting Rows with WHERE Case sensitive; single quotes

12 12 SQL> SELECT ename, hiredate 2 FROM emp 3 WHERE hiredate >= ’01-JAN-1982'; SQL> SELECT ename, hiredate 2 FROM emp 3 WHERE hiredate >= ’01-JAN-1982'; WHERE Clause Criteria  Text and dates are enclosed in single quotes  Numbers are not enclosed in quotes  Text values are case sensitive  Date values are format sensitive (Our Oracle default date format is DD-MON-YYYY)

13 13 SQL Comparison Operators

14 14 SQL Comparison Operators

15 15 ENAME SAL ---------- --------- MARTIN 1250 TURNER 1500 WARD 1250 ADAMS 1100 MILLER 1300 SQL> SELECT ename, sal 2 FROM emp 3 WHERE sal BETWEEN 1000 AND 1500; BETWEEN Operator Example

16 16 ENAME JOB ---------- --------- KING PRESIDENT BLAKE MANAGER CLARK MANAGER JONES MANAGER SQL> SELECT ename, job 2 FROM emp 3 WHERE job IN(‘PRESIDENT’,’MANAGER’); IN Operator Example

17 17 ENAME ---------- JONES JAMES SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE ‘J%’; LIKE Operator Example The % is a wildcard character that stands for zero to many characters. The underscore character (_) can be used to stand for exactly one character. The LIKE comparison operator is used when using wildcards. ( LIKE is not the same as = )

18 18 ENAME MGR ---------- --------- KING SQL> SELECT ename, mgr 2 FROM emp 3 WHERE mgr IS NULL; IS NULL Operator Example

19 19 Boolean (Logical) Operators

20 20 ENAME JOB SAL ---------- --------- --------- ADAMS CLERK 1100 MILLER CLERK 1300 SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE job = ‘CLERK’ 4 AND sal > 1000; AND Operator Example Both conditions must be true

21 21 ENAME ---------- JONES MARTIN JAMES MILLER SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE ‘J%’ 4 OR ename LIKE ‘M%’; OR Operator Example At least one condition must be true

22 22 JOB --------- ANALYST MANAGER PRESIDENT SALESMAN SQL> SELECT DISTINCT job 2 FROM emp 3 WHERE job NOT LIKE ‘C%’; NOT Operator Example

23 23 Arithmetic Operators

24 24 ENAME SAL ANNUAL ---------- --------- --------- JAMES 950 11400 SMITH 800 9600 ADAMS 1100 13200 MILLER 1300 15600 SQL> SELECT ename, sal, sal*12 AS annual 2 FROM emp 3 WHERE job = ‘CLERK’; Arithmetic Expression Example

25 25 Aggregate Functions

26 26 SALARY AVG ---------- 1400 SQL> SELECT AVG(sal) AS “SALARY AVG” 2 FROM emp 3 WHERE job = ‘SALESMAN’; Aggregate Function Example

27 27 COUNT(*) --------- 14 SQL> SELECT COUNT(*) 2 FROM emp; Aggregate Function Example Note: COUNT(*) returns the number of rows in a table while COUNT(field) returns the number of rows that are nonnull for the field counted

28 28 ENAME JOB SAL ---------- --------- --------- ADAMS CLERK 1100 ALLEN SALESMAN 1600 BLAKE MANAGER 2850 CLARK MANAGER 2450 FORD ANALYST 3000......... SQL> SELECT ename, job, sal 2 FROM emp 3 ORDER BY ename; Sorting Rows with ORDER BY

29 29 ENAME JOB SAL ---------- --------- --------- FORD ANALYST 3000 SCOTT ANALYST 3000 ADAMS CLERK 1100 JAMES CLERK 950 MILLER CLERK 1300......... SQL> SELECT ename, job, sal 2 FROM emp 3 ORDER BY job, ename; Sorting by Multiple Fields The order of the list determines the precedence of the sort order

30 30 ENAME JOB SAL ---------- --------- --------- KING PRESIDENT 5000 FORD ANALYST 3000 SCOTT ANALYST 3000 JONES MANAGER 2975 BLAKE MANAGER 2850......... SQL> SELECT ename, job, sal 2 FROM emp 3 ORDER BY sal DESC; Sorting in Descending Order

31 31 JOB AVG(SAL) --------- ANALYST 3000 CLERK 1037.5 MANAGER 2758.3333 PRESIDENT 5000 SALESMAN 1400 SQL> SELECT job, AVG(sal) 2 FROM emp 3 GROUP BY job; Categorizing with GROUP BY IMPORTANT NOTE: Any field or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause

32 32 DEPTNO COUNT(*) --------- ---------- 10 3 20 5 30 6 SQL> SELECT deptno, COUNT(*) 2 FROM emp 3 GROUP BY deptno; Categorizing with GROUP BY

33 33 DEPTNO COUNT(*) --------- ---------- 20 5 30 6 SQL> SELECT deptno, COUNT(*) 2 FROM emp 3 GROUP BY deptno 4 HAVING COUNT(*) >= 5; Limiting GROUP BY with HAVING HAVING acts similarly to a WHERE clause, but operates on groups, not on individual records

34 34 Processing Multiple Tables  When relationships exist between tables, the tables can be linked together in queries  Relationships between tables are established by setting up primary key to foreign key relationships between columns that are common to both tables  We link related tables together in SQL queries by using joins

35 35 Joins  A join is defined as: “A relational operation that causes two tables with a common domain to be combined into a single table”  A join is specified in SQL by using a WHERE clause to match values for the common field between the two tables (if you were joining three tables, you would need two joining WHERE clauses)  Each row in the resultant output table contains data from rows in the input tables where values for the common field match

36 36 EMPNO DEPTNO LOC ----- ------- -------- 7839 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLAS 7654 30 CHICAGO 7499 30 CHICAGO......... EMPNO DEPTNO LOC ----- ------- -------- 7839 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLAS 7654 30 CHICAGO 7499 30 CHICAGO......... EMP EMPNOENAME...DEPTNO -----------...------ 7839KING... 10 7698BLAKE... 30 7782CLARK... 10 7566JONES... 20............ DEPT DEPTNO DNAME LOC ------ ------------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON Data from Multiple Tables DEPTNO is the common field joining the EMP and DEPT tables

37 37 SELECTtable1.field, table2.field,... FROMtable1, table2 WHEREtable1.fieldX = table2.fieldX; SELECTtable1.field, table2.field,... FROMtable1, table2 WHEREtable1.fieldX = table2.fieldX; Creating a Join in SQL  Write the join condition in the WHERE clause  Prefix the column name with the table name when the same column name appears in more than one table (to avoid ambiguity) SELECTempno, emp.deptno, loc FROMemp, dept WHEREemp.deptno = dept.deptno; SELECTempno, emp.deptno, loc FROMemp, dept WHEREemp.deptno = dept.deptno; This is the query that is shown on the previous slide

38 38 ORDERDATE QTY DESCRIP --------- --------- ------------------------------ 07-JAN-87 1 ACE TENNIS NET 11-JAN-87 1 ACE TENNIS RACKET II 15-JAN-87 100 ACE TENNIS RACKET I 01-MAY-86 1 SB ENERGY BAR-6 PACK 05-JUN-86 20 ACE TENNIS BALLS-3 PACK 15-JUN-86 3 ACE TENNIS NET......... SQL> SELECT ord.orderdate, item.qty, product.descrip 2 FROM ord, item, product 3 WHERE ord.ordid = item.ordid 4 AND item.prodid = product.prodid; 3 Table Join Example Note: Table name prefixes are used here for all fields for clarity

39 39 Avoiding Cartesian Products  A Cartesian product is formed when a query on multiple tables is attempted and the join condition is omitted or is invalid  The result is that all rows in the first table are joined to all rows in the second table (e.g., if there were 10 rows in each table, the query would return 100 rows in the result)  To avoid a Cartesian product, always include a valid join condition in the WHERE clause of your SELECT statement

40 40 Join Types  Equi-join: “A join in which the joining condition is based on equality between values in the common columns. Common columns appear (redundantly) in the result table.”  Natural join: “Same as equi-join except one of the duplicate columns is eliminated in the result table.”  Outer join: “A join in which rows that do not have matching values in common columns are nevertheless included in the result table.”  Self join: A join that represents a recursive unary relationship of a table with itself.

41 41 ENAME DEPTNO DEPTNO DNAME ---------- --------- --------- -------------- KING 10 10 ACCOUNTING BLAKE 30 30 SALES CLARK 10 10 ACCOUNTING JONES 20 20 RESEARCH MARTIN 30 30 SALES ALLEN 30 30 SALES...... SQL> SELECT ename, emp.deptno, dept.deptno, dname 2 FROM emp, dept 3 WHERE emp.deptno = dept.deptno; Equi-join Example Note duplicate columns Note joining WHERE clause Note table name prefixes used to specify names for common fields

42 42 ENAME DEPTNO DNAME ---------- --------- -------------- KING 10 ACCOUNTING BLAKE 30 SALES CLARK 10 ACCOUNTING JONES 20 RESEARCH MARTIN 30 SALES ALLEN 30 SALES......... SQL> SELECT ename, emp.deptno, dname 2 FROM emp, dept 3 WHERE emp.deptno = dept.deptno; Natural Join Example Note elimination of duplicate column Note elimination of duplicate field

43 43 ENAME DEPTNO DNAME ---------- --------- -------------- KING 10 ACCOUNTING BLAKE 30 SALES CLARK 10 ACCOUNTING......... MILLER 10 ACCOUNTING SQL> SELECT ename, emp.deptno, dname 2 FROM emp, dept 3 WHERE emp.deptno = dept.deptno; Outer Join Example A total of 14 rows are returned First, let’s run this as a natural join

44 44 ENAME DEPTNO DNAME ---------- --------- -------------- KING 10 ACCOUNTING BLAKE 30 SALES CLARK 10 ACCOUNTING......... MILLER 10 ACCOUNTING OPERATIONS SQL> SELECT ename, emp.deptno, dname 2 FROM emp, dept 3 WHERE emp.deptno (+) = dept.deptno; Outer Join Example A total of 15 rows are returned Now, we’ll use an outer join, with Oracle notation (+) Included is this department that has no employees

45 45 Using Outer Joins  The outer join operator (+) can appear on only one side of the equal sign in the WHERE clause; it is placed on the side that is deficient in information  A condition involving an outer join cannot be linked to another condition by the OR operator or use the IN operator  ANSI standard syntax is different than Oracle’s (e.g., LEFT OUTER JOIN)

46 46 Self Joins  Used to join a table to itself  Useful to query a table involved in a recursive unary relationship with itself  Aliases are used in order to be able to distinguish the side of the relationship the table being referenced is on e.g., we could reference the emp table on the employee side of a “manages” unary relationship as “e” and the same table on the manager side of the relationship as “m”

47 47 EMPNO ENAME MGR --------- ---------- --------- 7839 KING 7698 BLAKE 7839 7782 CLARK 7839 7566 JONES 7839 7654 MARTIN 7698 7499 ALLEN 7698......... SQL> SELECT empno, ename, mgr 2 FROM emp; Preparation for Self Join Identify the common fields

48 48 ENAME MANAGER ---------- KING BLAKE KING CLARK KING JONES KING MARTIN BLAKE... SQL> SELECT e.ename, m.ename AS manager 2 FROM emp e, emp m 3 WHERE e.mgr = m.empno (+); Self Join Example Aliases Note: Aliases can be useful in other situations besides self joins. Any time you wish to abbreviate a table name you can use them

49 49 Subqueries  Subqueries are useful when a query is based on unknown values (e.g., “Who has a salary greater than Blake?” when Blake’s salary is unknown)  Subqueries involve placing an inner query (SELECT…FROM…WHERE...etc.) within a WHERE or HAVING clause of an outer query  The inner query is a complete query that could stand on its own and serves to provide values for the search condition of the outer query

50 50 ENAME ---------- KING JONES FORD SCOTT SQL> SELECT ename 2 FROM emp 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno = 7698); Subquery Example The inner query is enclosed in parentheses; indenting is optional The inner query determines the salary of Blake (empno = 7698) and returns it to the outer query

51 51 ENAME JOB SAL ---------- --------- --------- SMITH CLERK 800 SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE sal = 4 (SELECT MIN(sal) 5 FROM emp); Another Subquery Example The inner query determines the minimum salary of all employees and returns it to the outer query


Download ppt "SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]"

Similar presentations


Ads by Google