Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.

Similar presentations


Presentation on theme: "Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh."— Presentation transcript:

1 Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh

2 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

3 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

4 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

5 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

6 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

7 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

8 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 1 1 1 1 4 4 3 3 2 2 3 3 1 1 5 5 15 5 5 37 11 4 4 2 2 1003 buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia buyer_idbuyer_id 1 1 2 2 3 3 4 4 Resultbuyer_namebuyer_name Adam Barr Erin O’Melia Eva Corets buyer_idbuyer_idqtyqty 1 1 1 1 4 4 3 3 15 5 5 37 11 Erin O’Melia 4 4 1003 Example 1 Using Inner Joins

9 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

10 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 1 1 1 1 4 4 3 3 2 2 3 3 1 1 5 5 15 5 5 37 11 4 4 2 2 1003 buyersbuyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia buyer_idbuyer_id 1 1 2 2 3 3 4 4 Result buyer_namebuyer_name Adam Barr Erin O’Melia Eva Corets buyer_idbuyer_idqtyqty 1 1 1 1 4 4 3 3 15 5 5 37 11 Erin O’Melia 4 4 1003 Sean Chai NULL Example 1 Using Left Outer Joins

11 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

12 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

13 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

14 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 15 5 5 37 11 Adam Barr 1003 Sean Chai 15 Sean Chai 5 5 37 Sean Chai 11 Sean Chai 1003 Eva Corets 15... sales buyer_idbuyer_idprod_idprod_idqtyqty 1 1 1 1 4 4 3 3 2 2 3 3 1 1 5 5 15 5 5 37 11 4 4 2 2 1003 buyers buyer_idbuyer_id 1 1 2 2 3 3 4 4 buyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia Example 1 Using Cross Joins

15 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 1 1 2 2 3 3 4 4 Apples Pears Oranges Bananas 5 5 Peaches buyersbuyer_idbuyer_id 1 1 2 2 3 3 4 4 buyer_namebuyer_name Adam Barr Sean Chai Eva Corets Erin O’Melia salesbuyer_idbuyer_id 1 1 1 1 3 3 4 4 prod_idprod_id 2 2 3 3 1 1 5 5 2 2 2 2 qtyqty 15 5 5 37 11 1003 Result buyer_namebuyer_name Erin O’Melia Adam Barr Erin O’Melia Adam Barr Eva Corets prod_nameprod_name Apples Pears Oranges Peaches qtyqty 37 15 1003 5 5 11 Example 1

16 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 1 1 1 1 4 4 3 3 2 2 3 3 1 1 5 5 15 5 5 37 11 4 4 2 2 1003 sales a buyer_idbuyer_idprod_idprod_idqtyqty 1 1 1 1 4 4 3 3 2 2 3 3 1 1 5 5 15 5 5 37 11 4 4 2 2 1003 Result buyer1buyer1 4 4 prod_idprod_idbuyer2buyer2 2 2 1 1 Example 3

17 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

18 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

19 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

20 Slide 20 of 15


Download ppt "Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh."

Similar presentations


Ads by Google