Writing complex query Flashcards
In this complex query which will sql process first, the first or second SELECT query?
- Find products that are more
- expensive than lettuce (id = 3)
SELECT *
FROM products
WHERE unit_price > (
SELECT unit_price
FROM products
WHERE product_id = 3
)
parenthesis will process first, the subquery in this query
- Find products that are more
- expensive than lettuce (id = 3)
SELECT *
FROM products
WHERE unit_price > (
SELECT unit_price
FROM products
WHERE product_id = 3
)
True or False
A subquery is limited within a WHERE clause
False
- exercise
- In sql_hr database
- Find employees who earn more than average
SELECT *
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
)
notice the avg calculation is performed first in the parenthesis within a sub query
This query is returning what information
– Find products that have never been ordered
SELECT DISTINCT product_id
FROM order_items
A list of items that have been ordered by product_id
This query is returning what?
– Find products that have never been ordered
SELECT *
FROM products
WHERE product_id NOT IN (
SELECT DISTINCT product_id
FROM order_items
)
All the items in NOT IN the subquery list of distinct items ordered, so it’s excluding product_id sweet pea sprouts not being ordered.
Explain the steps in completing this exercise
– Find clients without invoices
SELECT *
FROM clients
WHERE client_id NOT IN (
SELECT distinct client_id
FROM invoices
)
- get all the distinct client_id in the invocies table
SELECT distinct client_id
FROM invoices
- then exclude the clients from the client table NOT IN this list created by the subquery
SELECT *
FROM clients
WHERE client_id NOT IN
True or False
Many subqueries and joins can be performed interchangeablely
True
Cite the two types of joins
inner and outter
Same results or not?
– SELECT *
– FROM clients
– WHERE client_id NOT IN (
– SELECT distinct client_id
– FROM invoices
– )
SELECT *
FROM clients
LEFT JOIN invoices USING (client_id)
WHERE invoice_id IS NULL
Same results
Given the tables involved explain this query
– Find customers who have ordered lettuce (id=3)
– Select customer_id, first_name, last_name
select *
from customers
where customer_id in (
select o.customer_id
from order_items oi
join orders o using (order_id)
where product_id = 3
)
subquery with a join on customer_id and order_id on the different tables
Given the same two table explain this query
select distinct customer_id,
first_name,
last_name
from customers c
join orders o using (customer_id)
join order_items oi using (order_id)
where oi.product_id = 3
The two joins here are clear and filtered result from the where clause, easier reading than a subquery route and distinct being used cause a customer appeared more than once in the query.
Memorize the steps here and understand the question being asked.
- select invoices larger than all invoices of
- client 3
Returning all the clients that have a invoice_total > than client #3
select *
from invoices
where invoice_total > (
select max(invoice_total)
from invoices
where client_id = 3
)
Will this query work without the ALL clause?
– select invoices larger than all invoices of
– client 3
select *
from invoices
where invoice_total > all (
select invoice_total
from invoices
where client_id = 3
)
No, the ALL clause is calculating all the values instead of just returning a single value in a where clause
True or False
the MAX and ALL sql clauses are comprable
True
What is the count(*) clause doing here?
– select clients with at least two invoices
select *
from clients
where client_id IN (
select client_id
from invoices
group by client_id
having count(*) >= 2
)
count everything, can see the direct results by including it in the select clause
select client_id, count(*)
then remove the count(*) column with the not needed tally