SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)

Slides:



Advertisements
Similar presentations
Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
Chapter 5 Recursion in SQL. 2 Example. Let Flights(Flight#, Source_Ctiy, Dest_City) be a relational schema DEN CHI SFO DAL NY UA 930 DL 900 UA 1400 UA.
1 The Optimizer How ORACLE optimizes SQL statements David Konopnicky 1997, Revised by Mordo Shalom 2004.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 5 Advanced SQL. 2 Recursion in SQL Example. Let Flights(Flight#, Source_City, Dest_City) be a relational schema DEN CHI SFO DAL NY UA 930 DL 900.
Oracle Database Administration Lecture 2 SQL language.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
Subqueries.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
An Introduction To SQL - Part 1 (Special thanks to Geoff Leese)
SQL.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Aggregating Data Using Group Functions
Interacting with the Oracle Server
(SQL) Aggregating Data Using Group Functions
جملة الاستعلام الأساسية
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Multi-table queries Subqueries
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
(SQL) Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Restricting and Sorting Data
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Presentation transcript:

SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)

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

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

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)

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:

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 ;

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;

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;

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/22/ 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;

10/22/ 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

10/22/ 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

10/22/ 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;

10/22/ 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;

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

10/22/ 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.

10/22/ 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;

10/22/ 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';

10/22/ 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;

10/22/ 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