SQL Subqueries/Joins Flashcards
Week 9+10 content
How are rows in tables normally stored, and how is Database Index a better alternative?
Also the types of Database indexes?
Rows are stored in random order, allowing for no processing when being WRITTEN, but when you are READING from the table, every row must be checked.
Database Index allows for rows to be located faster.
Types of Indexes:
Non-clustered - index structure is separate from the table
Clustered - The table is organized as an index
What does B-Tree Index stand for?
What constraints automatically generate a B-Tree index?
B-Tree stands for balanced tree, note Azure Data Studio uses B-Tree indexes.
The UNIQUE and PRIMARY KEY constraints automatically generate a B-Tree index
Answer the following questions:
a. What is a subquery?
b. What are the four places we can use a subquery?
c. Subqueries can be either/or what 2 things?
a. A subquery is an inner query that exists in an outer query.
b. Condition in the WHERE clause,
as a table after the FROM clause,
Returning a column after the SELECT clause,
within the HAVING clause
c. Subqueries can be correlated or non-correlated
Given the following tables:
Employee(fName, salary, jobID)
Job(jobID, jobTitle)
Using Subqueries:
return the name and salary of all employees that are ‘Programmer’
SELECT fName, salary from Employee,
WHERE jobID = (SELECT jobID from Job where jobTitle = ‘Programmer’);
Given the following tables:
Employee(fName, salary, jobID)
Job(jobID, jobTitle)
Using Join:
return the name and salary of all employees that are ‘Programmer’
SELECT fName, salary from Employee e
JOIN Job j on e.jobID = j.jobID
WHERE j.jobTitle = ‘Programmer’;
What is the difference between a JOIN and a Subquery?
Join creates a cross product, essentially combining rows from the initial table with the joining table that share the same Primary Key(done using ON)
Subquery just operates and returns a result that can be used in the outer query.
Note: Both can be used to solve the same query, but they solve the same query in very distinct ways
What is a Cartesian product?
When using a CROSS JOIN without a WHERE condition, the two tables will become combined and the end result will be the # of rows returned will be equal to #of table1 rows times table2 rows.
Example:
SELECT * FROM Employees - returns 107 rows
SELECT * FROM Job - returns 27 rows
SELECT * FROM Employees. Job - returns 107*27 rows
Given the following table:
Employee(name, salary, emp_ID)
Using a Correlated Subquery:
return all info on the highest paid employee
SELECT * FROM Employee e1
WHERE e1.salary > ALL
(SELECT salary from Employee e2
WHERE e2.emp_ID != e1.emp_ID)
or
SELECT * FROM Employee,
WHERE salary = (SELECT max(salary) from Employee)
Given the tables:
Employee(name, emp_ID, department_ID)
Department(department_ID, department_name)
Using EXISTS:
return all employees that work in the ‘IT’ department
SELECT * FROM Employee E
WHERE EXISTS (Select * FROM DEPARTMENT d
WHERE d.department_ID = e.department_ID AND department_name = ‘IT’);
Given the table:
Employee(name, salary)
Using a derived table:
return the name, salary, and average salary and only show employees that have more than the average
SELECT name, salary, Avg_Salary FROM Employee e1,
(SELECT AVG(salary) as Avg_Salary from Employee e2)
WHERE salary > Avg_Salary
What does a UNION do?
Combine the output of multiple queries into one result set
Given the table:
Employee(name, salary)
Using CASE:
return all data from the employee table and add a column named salary_level where if the employee makes more than 10000, it displays ‘High salary’ otherwise it displays ‘Low salary’
SELECT name, salary, CASE WHEN salary > 10000 THEN ‘High salary’ ELSE ‘Low Salary’ END AS salary_level
Given the following Tables:
Countries(country_id, country_name, region_id)
Regions(region_id, region_name)
Using either subqueries or join:
List all countries in the region Europe
Using Subqueries:
SELECT * from Countries c
WHERE region_id = (SELECT region_id from Regions WHERE region_name = ‘Europe’)
Using JOINs:
SELECT country_id, country_name from Countries c
JOIN Regions r on c.region_id = r.region_id
WHERE region_name = ‘Europe’
Given the table:
Employee(name, salary)
A. using Noncorrelated subquery and the IN operator:
return all employees that make more than 13000
B. using Noncorrelated subquery and the ANY operator:
return all employees that make more than 13000
SELECT * from employees
WHERE salary IN (SELECT salary from employees WHERE salary > 13000)
SELECT * from employees
WHERE salary > ANY (SELECT salary from employees WHERE salary = 13000)
Note: salary > ANY, means that salary has to be greater than the lowest value returned by the subquery.
Name the 4 types of joins
(INNER) JOIN : returns records that intersect between tables.
note: INNER JOIN is the same as JOIN
LEFT (OUTER) JOIN
and
RIGHT (OUTER) JOIN: Returns all records from the LEFT or RIGHT table
FULL (OUTER JOIN): Returns all records from both tables