Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
Multiple Table Queries
© Abdou Illia MIS Spring 2014
Module 5: Joining Multiple Tables. Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets.
Chapter 4 Joining Multiple Tables
Module 4: Joining Data from Multiple Tables. Querying Multiple Tables by Using Joins Applying Joins for Typical Reporting Needs Combining and Limiting.
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
ZEIT2301 Design of Information Systems Multi-Table Queries in SQL School of Engineering and Information Technology Dr Kathryn Merrick.
Displaying Data from Multiple Tables
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
SQL Joins.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
Best Practices Transact-SQL Cont....  Combining Data from Multiple Tables Introduction to Joins Using Inner Joins Using Outer Joins Using Cross Joins.
Joins. Joins In order to maintain normalization in the database design it is necessary to break up data into separate tables. The data can then be re-associated.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
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.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
INFANL01-3 ANALYSE 3 WEEK 3 March 2015 Institute voor Communication, Media en Informatietechnology.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
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.
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.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Introduction to Structured Query Language (SQL) By Techandmate.comTechandmate.com Learn SQL Server With US.
Module 5: Joining Multiple Tables. Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
MySQL Joins MySQL joins are used to combine rows from two or more tables. Different SQL JOINs INNER JOIN: Returns all rows when there is at least one match.
03 | Querying Multiple Tables with Joins
Querying Multiple Tables
Writing Basic SQL SELECT Statements
JOINS (Joinining multiple tables)
Introduction To Structured Query Language (SQL)
Writing Basic SQL SELECT Statements
Introduction To Structured Query Language (SQL)
Displaying Data from Multiple Tables
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
Trainer: Bach Ngoc Toan– TEDU Website:
Presentation transcript:

Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh

Slide 2 (of 30) Topic & Structure of Lesson  Using Aliases for Table Names  Using Joins to Combining Data from Multiple Tables  Combining Multiple Result Sets

SQL Aliases  SQL aliases are used to temporarily rename a table or a column heading.  Basically aliases are created to make column names more readable.  SQL Alias Syntax for Columns SELECT column_name AS alias_name FROM table_name  SQL Alias Syntax for Tables SELECT column_name(s) FROM table_name AS alias_name

Using Aliases for Table Names  Example 1 (without an alias name)  Example 2 (with an alias name) USE joindb SELECT buyer_name, s.buyer_id, qty FROM buyers AS b INNER JOIN sales AS s ON b.buyer_id = s.buyer_id GO USE joindb SELECT buyer_name, s.buyer_id, qty FROM buyers AS b INNER JOIN sales AS s ON b.buyer_id = s.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO

SQL JOINs  An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.  Types of SQL JOINs:  INNER JOIN  LEFT JOIN  RIGHT JOIN  FULL JOIN  CROSS JOIN

Introduction to Joins  Selects Specific Columns from Multiple Tables  JOIN keyword specifies that tables are joined and how to join them  ON keyword specifies join condition  Queries Two or More Tables to Produce a Result Set  Use primary and foreign keys as join conditions  Use columns common to specified tables to join tables

SQL INNER JOIN Keyword  The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.  SQL INNER JOIN Syntax SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name

USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id GO salesbuyer_idbuyer_idprod_idprod_idqtyqty buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia buyer_idbuyer_id Resultbuyer_namebuyer_name Adam Barr Erin O’Melia Eva Corets buyer_idbuyer_idqtyqty Erin O’Melia Example 1 Using Inner Joins

SQL LEFT JOIN Keyword  The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.  SQL LEFT JOIN Syntax SELECT column_name(s) FROM table1 LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name

USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers LEFT OUTER JOIN sales ON buyers.buyer_id = sales.buyer_id GO USE joindb SELECT buyer_name, sales.buyer_id, qty FROM buyers LEFT OUTER JOIN sales ON buyers.buyer_id = sales.buyer_id GO salesbuyer_idbuyer_idprod_idprod_idqtyqty buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia buyer_idbuyer_id Result buyer_namebuyer_name Adam Barr Erin O’Melia Eva Corets buyer_idbuyer_idqtyqty Erin O’Melia Sean Chai NULL Example 1 Using Left Outer Joins

SQL RIGHT JOIN Keyword  The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.  SQL RIGHT JOIN Syntax SELECT column_name(s) FROM table1 RIGHT OUTER JOIN table2 ON table1.column_name=table2.column_name

SQL FULL OUTER JOIN Keyword  The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2).  The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.  SQL FULL OUTER JOIN Syntax SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name

SQL CROSS JOIN Keyword  The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table,  If no WHERE clause is used along with CROSS JOIN. This kind of result is called as Cartesian Product.  SQL FULL OUTER JOIN Syntax SELECT column_name(s) FROM table1 CROSS JOIN table2

USE joindb SELECT buyer_name, qty FROM buyers CROSS JOIN sales GO USE joindb SELECT buyer_name, qty FROM buyers CROSS JOIN sales GO Result buyer_namebuyer_name Adam Barr qtyqty Adam Barr 1003 Sean Chai 15 Sean Chai Sean Chai 11 Sean Chai 1003 Eva Corets sales buyer_idbuyer_idprod_idprod_idqtyqty buyers buyer_idbuyer_id buyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia Example 1 Using Cross Joins

Joining More Than Two Tables SELECT buyer_name, prod_name, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id INNER JOIN produce ON sales.prod_id = produce.prod_id GO SELECT buyer_name, prod_name, qty FROM buyers INNER JOIN sales ON buyers.buyer_id = sales.buyer_id INNER JOIN produce ON sales.prod_id = produce.prod_id GO produceprod_idprod_idprod_nameprod_name Apples Pears Oranges Bananas 5 5 Peaches buyersbuyer_idbuyer_id buyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia salesbuyer_idbuyer_id prod_idprod_id qtyqty Result buyer_namebuyer_name Erin O’Melia Adam Barr Erin O’Melia Adam Barr Eva Corets prod_nameprod_name Apples Pears Oranges Peaches qtyqty Example 1

Joining a Table to Itself SELECT a.buyer_id AS buyer1, a.prod_id,b.buyer_id AS buyer2 FROM sales AS a JOIN sales AS b ON a.prod_id = b.prod_id WHERE a.buyer_id > b.buyer_id GO SELECT a.buyer_id AS buyer1, a.prod_id,b.buyer_id AS buyer2 FROM sales AS a JOIN sales AS b ON a.prod_id = b.prod_id WHERE a.buyer_id > b.buyer_id GO sales b buyer_idbuyer_idprod_idprod_idqtyqty sales a buyer_idbuyer_idprod_idprod_idqtyqty Result buyer1buyer1 4 4 prod_idprod_idbuyer2buyer Example 3

Combining Multiple Result Sets  The SQL UNION operator combines the result of two or more SELECT statements.  SQL UNION Syntax SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2

Combining Multiple Result Sets  Each Query Must Have:  Similar data types  Same number of columns  Same column order in select list Example: SELECT (firstname + ' ' + lastname) AS name, city, postalcode FROM employees UNION SELECT companyname, city, postalcode FROM customers SELECT (firstname + ' ' + lastname) AS name, city, postalcode FROM employees UNION SELECT companyname, city, postalcode FROM customers

Slide 13 (of 30) JOINS: Some notes  Inner is default and is usually omitted  Outer is also often omitted but should be included as good practice

Slide 20 of 15