sql queries - joining, multi-tables, subqueries

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/18

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards

join

  • Joins 2 or more tables into one single table or view

2
New cards

equi-join

  • The columns that you are joining should have equal values

    • They will appear redundantly in the result table

3
New cards

inner join

  • An equi-join where the duplicate columns are eliminated

  • ex. To find all sales persons that have at least one order

    • The left table is the salesperson table

    • The right table is the order table

select *

from Salesperson_T sp;

 

select*

from Order_T o;

 

forms one single large table:

select*

from Salesperson_T sp inner join Order_T o on o.SalesPersonID=sp.SalespersonID

 

If you don't want to use the inner join function:

select *

from Salesperson_T sp,  Order_T o

 where sp.SalesPersonID=o.SalespersonID

 

4
New cards

ex. of inner join

Produce a summary of sales (price*quantity), group by product

 

select p.ProductDescription,sum(p.ProductStandardPrice*ol.OrderedQuantity) [Total Sales ($)]

from Product_T p inner join OrderLine_T ol on p.ProductID=ol.ProductID

group by p.ProductDescription

 

ex.

Produce a summary of sales, group by year

 

select year(o.orderDate) [year], sum(p.ProductStandardPrice*ol.OrderedQuantity) [Total Sales ($)]

from Order_T o inner join OrderLine_T ol on o.OrderID=ol.OrderID inner join Product_T p on ol.ProductID=p.ProductID

group by year(o.orderdate)

 

ex.

  • List product line names with the number of products and the average product price being higher than $200 per product line

 

select pl.ProductLineName, count(p.productid) [NbrofProducts], avg(p.Productstandardprice) [Average Price]

from Product_T p inner join ProductLine_T pl on p.ProductLineID=pl.ProductLineID

group by pl.ProductLineName

having avg(p.productstandardprice)>200

5
New cards

outer join

  • Can be left, right, full

  • A join where you don't have matching values but they are included in the result table

6
New cards

left join

  • It gives you all of the information on the left side of the join

    • The right side is treated as a characteristic of the left side

  • ex.

    • Finding all the salespersons and list their orders

select*

from Salesperson_T sp left join Order_T o on sp. SalespersonID=o.SalesPersonID

7
New cards

right join

  • It gives you all of the information on the right side of the join

    • The left side is treated as a characteristic Of the right side

  • ex

    • Find all orders and list the salesperson assigned to each order

select*

from Salesperson_T sp right join Order_T o on sp. SalespersonID=o.SalesPersonID

8
New cards

ex. of left join

List of salespersons that have not been a part of any order

 

select*

from Salesperson_T sp left join Order_T o on sp. SalespersonID=o.SalesPersonID

where o.OrderID is null

List of salespersons along with the number of orders that they served

 

select sp.SalespersonID, sp.SalespersonName, count(o.orderid) [Number of Orders Served]

from Salesperson_T sp left join Order_T o on sp. SalespersonID=o.SalesPersonID

group by sp. SalespersonID, sp.SalespersonName

9
New cards

full join

  • Find all of the things on the right side of the join and all of the things on the left side of the join

  • ex.

    • Find all the salespersons and each of the orders they served along with all of the orders that have no sales person either

 

select*

from Salesperson_T sp full join Order_T o To join multiple tables 2on sp. SalespersonID=o.SalesPersonID

 

10
New cards

to join multiple diff. tables

  • 2 methods

    • Inner join

    • Where

  • ex.

    • Find all the customers who purchased products that have table in the description

      • For this query you will need the customer, order, order line, product tables

    • inner join

select distinct c.*

from Customer_T c inner join Order_T o on c.CustomerId=o.CustomerID

inner join OrderLine_T ol on ol.OrderID=o.OrderID

inner join Product_T p on p.ProductID=ol.ProductID

where p.ProductDescription like '%table%'

 

  • Where

select distinct c.*

from Customer_T c, Order_T o, OrderLine_T ol, Product_T p

where c. CustomerId=o.CustomerID and o.OrderID=ol.OrderID and ol.ProductID=p.ProductID

and p.ProductDescription Produce a summary of sales group by product like '%table%'

<ul><li><p><strong>2 methods</strong></p><ul><li><p><strong>Inner join</strong></p></li><li><p><strong>Where</strong></p></li></ul></li><li><p>ex.</p><ul><li><p>Find all the customers who purchased products that have table in the description</p><ul><li><p>For this query you will need the customer, order, order line, product tables</p></li></ul></li><li><p>inner join</p></li></ul></li></ul><p><strong><u>select distinc</u></strong><u>t c.*</u></p><p><strong><u>from</u></strong><u> Customer_T c </u><strong><u>inner join</u></strong><u> Order_T o </u><strong><u>on</u></strong><u> c.CustomerId=o.CustomerID</u></p><p><strong><u>inner join</u></strong><u> OrderLine_T ol </u><strong><u>on</u></strong><u> ol.OrderID=o.OrderID</u></p><p><strong><u>inner join</u></strong><u> Product_T p </u><strong><u>on</u></strong><u> p.ProductID=ol.ProductID</u></p><p><strong><u>where</u></strong><u> p.ProductDescription </u><strong><u>like</u></strong><u> '%table%'</u></p><p><u>&nbsp;</u></p><ul><li><p><u>Where</u></p></li></ul><p><strong><u>select distinct</u></strong><u> c.*</u></p><p><strong><u>from</u></strong><u> Customer_T c, Order_T o, OrderLine_T ol, Product_T p</u></p><p><strong><u>where</u></strong><u> c. CustomerId=o.CustomerID </u><strong><u>and</u></strong><u> o.OrderID=ol.OrderID </u><strong><u>and</u></strong><u> ol.ProductID=p.ProductID</u></p><p><strong><u>and</u></strong><u> p.ProductDescription Produce a summary of sales group by product </u><strong><u>like</u></strong><u> '%table%'</u></p><p></p>
11
New cards

self join

  • 2 methods

    • Inner join

    • Where

  • ex.

    • Show all supervisors and the employees they supervise

    • inner join

select s.EmployeeID [SupervisorID], s.EmployeeName [supervisorname], e. EmployeeID, e.EmployeeName, e. EmployeeSupervisor

from Employee_T s inner join Employee_T e on e.EmployeeSupervisor=s.EmployeeID

  • where

select s.EmployeeID [SupervisorID], s.EmployeeName [supervisorname], e. EmployeeID, e.EmployeeName, e. EmployeeSupervisor

from Employee_T s, Employee_T e

where e. EmployeeSupervisor = s.EmployeeID

12
New cards

ex. of self join

List how many other employees every employee supervises

 

select s.EmployeeID,s.EmployeeName, count(e.EmployeeID) [Managing Power]

from Employee_T s left join Employee_T e on e.EmployeeSupervisor=s.EmployeeID

group by s.EmployeeID,s.EmployeeName

order by [Managing Power] desc

 

 

left join allows all employees to be listed instead of just the supervisors

ex.

List the names of the employees, employee birth date, manager name, managers birthdate for all employees that were born before their manager

  • Think of it in the sense where employees are older than their supervisor

 

select e.*

from Employee_T s left join Employee_T e on e.EmployeeSupervisor=s.EmployeeID

where s.EmployeeBirthDate>e.EmployeeBirthDate

13
New cards

sub query

  • Having an inner query inside an outer query

  • Processed before the outer query

  • There is another select statement inside the query:

    • As a condition of the where clause

    • As a table of the from clause

      • this gives a derived table

    • As the field you want to return from the select clause

    • In the having clause

  • Can be non correlated

    • Only used once for the entire outer query

    • They do not depend on data from the outer query

  • Can be correlated

    • Used once for every row returned by the outer query

    • The sub query always refers to an attribute referenced in the outer query

    • In this instance you use the operators EXISTS (true or false), ALL

    • ex.

select*

from OrderLine_T ol

where exists (select*

from Product_T p

where p.ProductID=ol.productid and p.ProductFinish='oak')

 

14
New cards

ex. a subquery in a where clause

List the name and the address of the customer who placed order #34

 

select *

from Customer_T c

where c.CustomerId=(select o.CustomerID*

from Order_T o

where o.OrderID=34);

 

 

List all the employees that have the same supervisor as Laura

 

select *

from Employee_T e

where e.EmployeeSupervisor=(select e1.EmployeeSupervisor*

from Employee_T e1

where e1.EmployeeName like '%laura%')

 

 

 

List all the products that have a price that is above average

 

select*

from Product_T p

where p.ProductStandardPrice>( select avg(p1.ProductStandardPrice)

from Product_T p1)

 

 

List the names and addresses of all the customers who made a purchase in the year 2018

 

select*

from Customer_T c

where c.CustomerId in (select o.CustomerID

from Order_T o

where year(o.OrderDate)=2018)

 

list customers with no orders

 

select*

from Customer_T c

where c.CustomerId not in (

select distinct o.CustomerID

from Order_T o)

 

 

list orders that have products with 3 then 4 in id

 

select *

from Order_T o

where o.OrderID in (select ol.OrderID

from OrderLine_T ol

where ol.ProductID=3)

and o.OrderID in (select ol.OrderID

from OrderLine_T ol

where ol.ProductID=4)

 

 

 

 

the subquery can run by itself

15
New cards

ex. a subquery in the from clause

 

list orders that have products with 3 then 4 in id

 

select*

from (select o.*

from Order_T o inner join OrderLine_T ol on o.OrderID=ol.OrderID

where ProductID=3) t3, (select o.*

from Order_T o inner join OrderLine_T ol on o.OrderID=ol.OrderID

where ProductID=4) t4

where t3.orderid=t4.orderid

16
New cards

ex. subqueries using inner joining

list most expensive product of each product line

 

  1.  

select*

from Product_T p

where p.ProductStandardPrice=(select max(pl.productstandardprice)

from Product_T pl

where pl.ProductLineID=p.ProductLineID)

 

 

 

list oldest employee of each supervisor

 

  1.  

select *

from Employee_T e

where e.EmployeeBirthDate=(select min(es.employeebirthdate)

from Employee_T es

where es.EmployeeBirthDate=e.employeebirthdate)

 

 

employee info for all employees hired in each state before most recently hired person in that state

 

  1.  

select*

from Employee_T e

where e.EmployeeDateHired < (select max(e1.EmployeeDateHired)

from Employee_T e1

where e1.EmployeeState=e.employeestate)

 

17
New cards

intersect

ex.

 

Orders that have the products with the ID three and four in consecutive order

 

select o.*

from Order_T o inner join OrderLine_T ol on o.OrderID=ol.OrderID

where ol.ProductID=3

intersect

select o.*

from Order_T o inner join OrderLine_T ol on o.OrderID=ol.OrderID

where ol.ProductID=4

18
New cards

union

Orders that have the products with the ID #3 or 4

 

select o.*

from Order_T o inner join OrderLine_T ol on o.OrderID=ol.OrderID

where ol.ProductID=3

union

select o.*

from Order_T o inner join OrderLine_T ol on o.OrderID=ol.OrderID

where ol.ProductID=4

19
New cards

except

Orders that have a product ID with the number 3 but not have the number four in the same order

 

select o.*

from Order_T o inner join OrderLine_T ol on o.OrderID=ol.OrderID

where ol.ProductID=3

except

select o.*

from Order_T o inner join OrderLine_T ol on o.OrderID=ol.OrderID

where ol.ProductID=4