Presentation is loading. Please wait.

Presentation is loading. Please wait.

SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)

Similar presentations


Presentation on theme: "SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)"— Presentation transcript:

1 SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)

2 10/22/20152 SELECT Statements Retrieve data from one or more tables, object tables, views, object views, or snapshots Must have SELECT privilege Examples

3 10/22/20153 Examples Selects rows from the employee table with the department number of 40. SELECT * FROM emp WHERE deptno = 40

4 10/22/20154 Examples selects the name, job, salary and department number of all employees except salesmen from department number 30: SELECT ename, job, sal, deptno FROM emp WHERE NOT (job = 'SALESMAN' AND deptno = 30)

5 10/22/20155 Examples Selects from subqueries in the FROM clause and gives departments total employees and salaries as a percentage of all the departments:

6 10/22/20156 SELECT a.deptno "Department", a.num_emp/b.total_count "%Employees", a.sal_sum/b.total_sal "%Salary” FROM (SELECT deptno, COUNT(*) num_emp, SUM(SAL) sal_sum FROM scott.emp GROUP BY deptno) a, (SELECT COUNT(*) total_count, SUM(sal) total_sal FROM scott.emp) b ;

7 10/22/20157 Group by Examples Return the minimum and maximum salaries for each department in the employee table, issue the following statement. SELECT deptno, MIN(sal), MAX(sal) FROM emp GROUP BY deptno;

8 10/22/20158 Group by examples Return the minimum and maximum salaries for the clerks in each department, issue the following statement. SELECT deptno, MIN(sal), MAX(sal) FROM emp WHERE job = 'CLERK’ GROUP BY deptno;

9 10/22/20159 HAVING Clause Restrict which groups of rows defined by the GROUP BY clause. Oracle removes all rows that do not satisfy WHERE clause, calculates and forms the groups as specified in the GROUP BY clause, removes all groups that do not satisfy the HAVING clause.

10 10/22/201510 Having clause Return the minimum and maximum salaries for the clerks in each department whose lowest salary is below $1,000. SELECT deptno, MIN(sal), MAX(sal) FROM emp WHERE job = 'CLERK' GROUP BY deptno HAVING MIN(sal) < 1000;

11 10/22/201511 ORDER BY Clause order the rows selected by a query can specify multiple expressions in the ORDER BY clause sorts rows based on their values for the first expression

12 10/22/201512 Order by clause restrictions compound queries (containing set operators UNION, INTERSECT, MINUS, or UNION ALL), the ORDER BY clause must use positions, rather than explicit expressions cannot appear in subqueries within other statements

13 10/22/201513 Order by examples Select all salesmen's records from EMP, and order the results by commission in descending order. SELECT * FROM emp WHERE job = 'SALESMAN' ORDER BY comm DESC;

14 10/22/201514 Order by examples Select the employees from EMP ordered first by ascending department number and then by descending salary. SELECT ename, deptno, sal FROM emp ORDER BY deptno ASC, sal DESC;

15 10/22/201515 Order by examples Use the positional ORDER BY notation SELECT ename, deptno, sal FROM emp ORDER BY 2 ASC, 3 DESC;

16 10/22/201516 Joins A query that combines rows from two or more tables, views, or snapshots Performs a join whenever multiple tables appear in the query's FROM clause List any columns from any of these tables If any these tables have a common column name, these columns must be prefixed with table names to avoid ambiguity.

17 10/22/201517 Joins Examples Returns the name and job of each employee and the number and name of the department in which the employee works. SELECT ename, job, dept.deptno, dname FROM emp, dept WHERE emp.deptno = dept.deptno;

18 10/22/201518 Joins examples Returns the name, job, department number, and department name of all clerks. SELECT ename, job, dept.deptno, dname FROM emp, dept WHERE emp.deptno = dept.deptno AND job = 'CLERK';

19 10/22/201519 Joins: Self Joins Returns the name of each employee along with the name of the employee's manager. SELECT e1.ename||' works for '||e2.ename "Employees and their Managers" FROM emp e1, emp e2 WHERE e1.mgr = e2.empno;

20 10/22/201520 Joins: Cartesian Products have no join condition combines each row of one table with each row of the other generates many rows and is rarely useful


Download ppt "SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)"

Similar presentations


Ads by Google