DQL Statements Lab - 3 COMP 353 Summer 2013-14.

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
Database technology Lecture 2: Relational databases and SQL
Database Design -- Basic SQL
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Introduction to SQL Structured Query Language Martin Egerhill.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
Relational Algebra - Chapter (7th ed )
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
Structured Query Language
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Advanced Relational Algebra & SQL (Part1 )
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –continue with SQL (chapter.
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
1 COMP 1100 Basic SQL David J. Stucki. Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Chapter 11 SQL: Data Manipulation
More SQL: Complex Queries,
CS580 Advanced Database Topics
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter 4 Basic SQL.
6/22/2018.
CS580 Advanced Database Topics
Working with Tables: Join, Functions and Grouping
376a. Database Design Dept. of Computer Science Vassar College
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Joining Tables ضم الجداول وإستخراج مناظر views منها الهدف : 1- استخراج المعلومات من جدولين أو اكثر بالإستفادة من الرابط بينهما وبإستخدام SQL 2- شروط قواعد.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Company Requirements.
JOINS (Joinining multiple tables)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Contents Preface I Introduction Lesson Objectives I-2
Displaying Data from Multiple Tables
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
Presentation transcript:

DQL Statements Lab - 3 COMP 353 Summer 2013-14

JOINS : Joining Multiple Tables Tables are joined on columns that have the same data type and data width in the tables. After joining multiple tables we can work with them as though they were a single entity. Types of Joins: NATURAL JOIN INNER JOIN OUTERJOIN (LEFT, RIGHT) CROSS JOIN

SYNTAX : SELECT <Col1> , <Col2>, <ColN> JOINS : NATURAL JOIN A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on the common columns in the two tables being joined [1]. Common columns are columns that have the same name in both tables [1]. SYNTAX : SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> NATURAL JOIN <TableName2> WHERE <Condition> ORDER BY <Col1>, <Col2>, …. <ColN>

JOINS : NATURAL JOIN EXAMPLE: mysql> SELECT * FROM EMPLOYEE NATURAL JOIN DEPARTMENT; +-----------+-------+-------+------------+----------+----------+----------------+---------+----------------+ | Ssn | Fname | Lname | Bdate | Address | Salary | Dname | Dnumber | Mgr_start_date | | 433980990 | TIM | DUGAY | 1998-12-24 | Montreal | 24000.00 | Finance | 1 | 1998-12-24 | | 444333555 | Anna | DUGAY | 1996-10-14 | Toronto | 25000.00 | Marketing | 3 | 1998-12-24 | | 909090999 | Jai | Singh | 1990-02-24 | Montreal | 22000.00 | Human Resource | 2 | 1998-12-24 | 3 rows in set (0.00 sec)

JOINS : INNER JOIN Inner joins by far the most commonly used joins. This type of join can be used in situations where it is required to select only those rows that have values in common in the column specified in the ON clause. Inner join returns all rows from both tables where there is a match. Specifies a join between two tables with an explicit join clause [1]. SYNTAX : SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> INNER JOIN <TableName2> ON <TableName1>.<Col> = <TableName2>.<Col> WHERE <Condition> ORDER BY <Col1>, <Col2>, …. <ColN>

JOINS : INNER JOIN EXAMPLE: mysql> SELECT CONCAT(E.Fname,' ',E.Lname) AS Employee, E.Ssn, E.Salary, D.Dname AS 'Department-Manager' FROM EMPLOYEE AS E INNER JOIN DEPARTMENT AS D ON E.Ssn = D.Ssn; ORDER BY Salary DESC; +------------+-----------+----------+--------------------+ | Employee | Ssn | Salary | Department-Manager | | TIM DUGAY | 433980990 | 24000.00 | Finance | | Anna DUGAY | 444333555 | 25000.00 | Marketing | | Jai Singh | 909090999 | 22000.00 | Human Resource | 3 rows in set (0.00 sec) Key Points (Apart from INNER JOIN): Renaming an Attribute. Concatenating 2 Attributes. Table Aliases to remove column ambiguity (To remove following error). (ERROR 1052 (23000): Column 'Ssn' in field list is ambiguous)

JOINS : LEFT OUTER JOIN Outer joins gives bit more flexibility when selecting data from related tables. This type of join can be used in situations where it is desired, to select all rows from the table on the left regardless of whether the other table has values in common and enter null where data is missing. A LEFT OUTER JOIN is one of the JOIN operations that allow you to specify a join clause. It preserves the unmatched rows from the first (left) table, joining them with a NULL row in the shape of the second (right) table [1]. SYNTAX : SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> LEFT JOIN <TableName2> ON <TableName1>.<Col> = <TableName2>.<Col> WHERE <Condition> ORDER BY <Col1>, <Col2>, …. <ColN>

JOINS : LEFT OUTER JOIN EXAMPLE: mysql> SELECT Fname,Lname,EMPLOYEE.Ssn, Salary, Dname AS 'Manager?' FROM EMPLOYEE LEFT JOIN DEPARTMENT ON EMPLOYEE.Ssn = DEPARTMENT.Ssn ORDER BY Salary DESC; +---------+----------+-----------+----------+----------------+ | Fname | Lname | Ssn | Salary | Manager? | | Tristan | McDonald | 111111111 | 34000.00 | NULL | | Mathew | Munger | 222233332 | 28000.00 | NULL | | Anna | DUGAY | 444333555 | 25000.00 | Marketing | | TIM | DUGAY | 433980990 | 24000.00 | Finance | | Jai | Singh | 909090999 | 22000.00 | Human Resource | 5 rows in set (0.01 sec)

JOINS : CROSS JOIN A CROSS JOIN is a JOIN operation that produces the Cartesian product of two tables. SYNTAX : SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> CROSS JOIN <TableName2> WHERE <Condition> ORDER BY <Col1>, <Col2>, …. <ColN> OR SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> , <TableName2>

JOINS : CROSS JOIN EXAMPLE: mysql> SELECT * FROM EMPLOYEE CROSS JOIN DEPARTMENT; OR SELECT * FROM EMPLOYEE,DEPARTMENT; +---------+----------+-----------+------------+----------+----------+----------------+---------+-----------+----------------+ | Fname | Lname | Ssn | Bdate | Address | Salary | Dname | Dnumber | Ssn | Mgr_start_date | | Tristan | McDonald | 111111111 | 1998-12-24 | Montreal | 34000.00 | Finance | 1 | 433980990 | 1998-12-24 | | Tristan | McDonald | 111111111 | 1998-12-24 | Montreal | 34000.00 | Human Resource | 2 | 909090999 | 1998-12-24 | | Tristan | McDonald | 111111111 | 1998-12-24 | Montreal | 34000.00 | Marketing | 3 | 444333555 | 1998-12-24 | | Mathew | Munger | 222233332 | 1998-12-24 | OTTAWA | 28000.00 | Finance | 1 | 433980990 | 1998-12-24 | | Mathew | Munger | 222233332 | 1998-12-24 | OTTAWA | 28000.00 | Human Resource | 2 | 909090999 | 1998-12-24 | | Mathew | Munger | 222233332 | 1998-12-24 | OTTAWA | 28000.00 | Marketing | 3 | 444333555 | 1998-12-24 | | TIM | DUGAY | 433980990 | 1998-12-24 | Montreal | 24000.00 | Finance | 1 | 433980990 | 1998-12-24 | | TIM | DUGAY | 433980990 | 1998-12-24 | Montreal | 24000.00 | Human Resource | 2 | 909090999 | 1998-12-24 | | TIM | DUGAY | 433980990 | 1998-12-24 | Montreal | 24000.00 | Marketing | 3 | 444333555 | 1998-12-24 | | Anna | DUGAY | 444333555 | 1996-10-14 | Toronto | 25000.00 | Finance | 1 | 433980990 | 1998-12-24 | | Anna | DUGAY | 444333555 | 1996-10-14 | Toronto | 25000.00 | Human Resource | 2 | 909090999 | 1998-12-24 | | Anna | DUGAY | 444333555 | 1996-10-14 | Toronto | 25000.00 | Marketing | 3 | 444333555 | 1998-12-24 | | Jai | Singh | 909090999 | 1990-02-24 | Montreal | 22000.00 | Finance | 1 | 433980990 | 1998-12-24 | | Jai | Singh | 909090999 | 1990-02-24 | Montreal | 22000.00 | Human Resource | 2 | 909090999 | 1998-12-24 | | Jai | Singh | 909090999 | 1990-02-24 | Montreal | 22000.00 | Marketing | 3 | 444333555 | 1998-12-24 | 15 rows in set (0.00 sec)

GROUPING DATA FROM TABLES IN SQL Till Now, all SQL SELECT statements have: Retrieved all the rows from tables. Retrieved selected rows from tables with the use of a WHERE clause which returns only rows that meet the condition specified. Retrieved unique rows from tables using DISTINCT clause. Retrieved sorted rows using ORDER BY clause. GROUP BY and HAVING Clauses Other than above mentioned clauses these 2 clauses also facilitate selective retrieval of rows. These 2 clauses act on result set rather than individual records.

GROUP BY Clause GROUP BY clause is another section of the SELECT statement. It is an optional clause that tells MYSQL to group rows based on distinct values that exist for specified columns. SYNTAX : SELECT <Col1> , <Col2>, <ColN>, AGGREGATE_FUNCTION(exp) FROM <TableName1> WHERE <Condition> GROUP BY <Col1>, <Col2>, …. <ColN> ORDER BY <Col1>, <Col2>, …. <ColN> ;

GROUP BY Clause EXAMPLE: mysql> SELECT City, COUNT(*) FROM EMPLOYEE GROUP BY City; +----------+----------+ | City | COUNT(*) | | Montreal | 4 | | OTTAWA | 1 | | Toronto | 1 | 3 rows in set (0.00 sec)

HAVING Clause The HAVING clause can be used in conjunction with the GROUP BY Clause. HAVING clause imposes a condition on the GROUP BY clause, which further filters the groups created by the GROUP BY clause. SYNTAX : SELECT <Col1> , <Col2>, <ColN>, AGGREGATE_FUNCTION(exp) FROM <TableName1> WHERE <condition> GROUP BY <Col1>, <Col2>, …. <ColN> HAVING <condition> ORDER BY <Col1>, <Col2>, …. <ColN> ;

HAVING Clause EXAMPLE: mysql> SELECT City, COUNT(*) FROM EMPLOYEE GROUP BY City HAVING COUNT(*)>2; +----------+----------+ | City | COUNT(*) | | Montreal | 4 | 1 row in set (0.00 sec) EXAMPLE: mysql> SELECT City, COUNT(*) AS EmployeeCount FROM EMPLOYEE GROUP BY City HAVING EmployeeCount>2; +----------+---------------+ | City | EmployeeCount | | Montreal | 4 | 1 row in set (0.00 sec)

DQL Statement : Logical Operator (AND) The AND operator requires that each condition must be met for the record to be included in the Result Set. SYNTAX : SELECT * from <TableName> WHERE <condition1> AND <condition2>…; EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Ssn>400000000 AND Salary>23000; +-------+-------+-----------+------------+----------+----------+ | Fname | Lname | Ssn | Bdate | Address | Salary | | TIM | DUGAY | 433980990 | 1998-12-24 | Montreal | 24000.00 | | Anna | DUGAY | 444333555 | 1996-10-14 | Toronto | 25000.00 | 2 rows in set (0.01 sec)

DQL Statement : Logical Operator (OR) The OR operator requires that any of the conditions must be met for the record to be included in the Result Set. SYNTAX : SELECT * from <TableName> WHERE <condition1> OR <condition2>…; EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Ssn>400000000 OR Salary>23000; +---------+----------+-----------+------------+----------+----------+ | Fname | Lname | Ssn | Bdate | Address | Salary | | Tristan | McDonald | 111111111 | 1998-12-24 | Montreal | 34000.00 | | Mathew | Munger | 222233332 | 1998-12-24 | OTTAWA | 28000.00 | | TIM | DUGAY | 433980990 | 1998-12-24 | Montreal | 24000.00 | | Anna | DUGAY | 444333555 | 1996-10-14 | Toronto | 25000.00 | | Jai | Singh | 909090999 | 1990-02-24 | Montreal | 22000.00 | 5 rows in set (0.00 sec)

DQL Statement : Range Searching (BETWEEN Operator) The BETWEEN operator is used to select data that is within a range of values. Range Specified after the BETWEEN operator is inclusive. SYNTAX : SELECT * from <TableName> WHERE <colName> BETWEEN <val1> AND <val2>; EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Salary BETWEEN 26000 AND 34000; +---------+----------+-----------+------------+----------+----------+ | Fname | Lname | Ssn | Bdate | Address | Salary | | Tristan | McDonald | 111111111 | 1998-12-24 | Montreal | 34000.00 | | Mathew | Munger | 222233332 | 1998-12-24 | OTTAWA | 28000.00 | 2 rows in set (0.00 sec)

DQL Statement : (IN and NOT IN Predicate) In case a value needs to be compared to a list of values then the IN predicate is used. SYNTAX : SELECT * from <TableName> WHERE <colName> IN (val1>,<val2>,<val3>,….,<valn>); EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Address IN ('Toronto','Vancouver'); +-------+-------+-----------+------------+---------+----------+ | Fname | Lname | Ssn | Bdate | Address | Salary | | Anna | DUGAY | 444333555 | 1996-10-14 | Toronto | 25000.00 | 1 row in set (0.00 sec) mysql> SELECT * from EMPLOYEE WHERE Address NOT IN ('Toronto','Vancouver'); +---------+----------+-----------+------------+----------+----------+ | Fname | Lname | Ssn | Bdate | Address | Salary | | Tristan | McDonald | 111111111 | 1998-12-24 | Montreal | 34000.00 | | Mathew | Munger | 222233332 | 1998-12-24 | OTTAWA | 28000.00 | | TIM | DUGAY | 433980990 | 1998-12-24 | Montreal | 24000.00 | | Jai | Singh | 909090999 | 1990-02-24 | Montreal | 22000.00 | 4 rows in set (0.00 sec)

DQL Statement : (ALL Operator) The word ALL, which must follow a comparison operator, means “return TRUE if the comparison is TRUE for ALL of the values in the column that the subquery returns [1] SYNTAX : operand comparison_operator ALL (subquery) EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Salary > ALL (SELECT min(Salary) from EMPLOYEE); +---------+----------+-----------+------------+----------+----------+ | Fname | Lname | Ssn | Bdate | Address | Salary | | Tristan | McDonald | 111111111 | 1998-12-24 | Montreal | 34000.00 | | Mathew | Munger | 222233332 | 1998-12-24 | OTTAWA | 28000.00 | | TIM | DUGAY | 433980990 | 1998-12-24 | Montreal | 24000.00 | | Anna | DUGAY | 444333555 | 1996-10-14 | Toronto | 25000.00 | 4 rows in set (0.00 sec)

DQL Statement : (ANY Operator) The ANY keyword, which must follow a comparison operator, means “return TRUE if the comparison is TRUE for ANY of the values in the column that the subquery returns.” [1] SYNTAX : operand comparison_operator ANY (subquery) EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Ssn = ANY(Select Mgr_ssn from DEPARTMENT WHERE Ssn > '500000000'); +-------+-------+-----------+------------+----------+----------+ | Fname | Lname | Ssn | Bdate | Address | Salary | | Jai | Singh | 909090999 | 1990-02-24 | Montreal | 22000.00 | 1 row in set (0.00 sec)

References 1 http://docs.oracle.com/javadb/10.8.2.2/ref/rrefsqljnaturaljoin.html#rrefsqljnaturaljoin 2. http://dev.mysql.com/doc/refman/5.0/en/data-type-overview.html