16. Subqueries Flashcards

(20 cards)

1
Q

DBMS Server has two types of storage:
1. Cache
2. Disk

The Disk storage has 3 partitions. List their name & purpose.

A
  1. User Data (databases, tables)
  2. System Catalog (metadata)
  3. Temp Storage (temporary tables)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The metadata in System Catalog is available for viewing through _ .

A

Information Schema

It has built-in views that provide information (metadata) about databases and tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Subquery result is computed by DB engine and stored in Cache. It is accessible only inside the main query and is discarded after the execution of main query. Any external query _ access that result.

A

cannot

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Based on dependency, subqueries can be classified into two categories.
Name them.

A
  1. Correlated Subquery
  2. Non-correlated Subquery
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Based on result type, subqueries can be classified into three categories.
Name them.

A
  1. Scalar Subquery (returns single aggregate value)
  2. Row Subquery (returns rows of a single column)
  3. Table Subquery (returns tabular result)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

– Find all orders with sales that are higher than the global average sales.

select
  OrderID,
  Sales,
  meanSales
from
(select
OrderID,
Sales,
avg(Sales) over() as meanSales
from MyOrders)t
where Sales > meanSales;

Here t is a _ in the from clause.

A

subquery

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Scalar subquery is used in select clause to display result from _ table.

Show ProductID, product name, price from Products table along with total number of orders.

A

different

select
  ProductID,
	Product,
	Price
	(select count(*) from Orders) as totalOrders
	from Product;

Here, totalOrders is a subquery in select clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Table subquery is used in join clause to bring the data in required shape _ applying join.

Find all customer names from Customers table who made an order (in Orders table).

A

before

select c.name
from Customers join
(select distinct customerId
from Orders)o
on c.customerId = o.customerId;

Here o is a subquery in join clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Scalar subquery is used in where clause for _ filtering.

A

dynamic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Row Subquery is used in IN clause inside the where clause for filtering across a _ of choices.

Show the details of orders made by customers in Germany.

Note: Orders table has orderID and customerID.
Countries table has cutomerID and country.

A

list

select orderId, customerId
from Orders
where customerId in
(select customerId from Countries where country = 'Germany');
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Show id, name and totalSales of orders made by each customer in Germany.
Note: MyOrders table has orderID and customerID.
customers table has id and country.

A
select o.CustomerID, c.first_name, o.totalSales
from (select CustomerID, sum(Sales) as totalSales from MyOrders group by CustomerID) o
join (select * from customers where country = 'Germany') c 
on c.id = o.customerID;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Show id, name and totalSales of orders made by each customer in Germany.
but instead of join, use subquery in where clause.

Hint: where ... in clause takes row subquery (single column)

A
select
  CustomerID,
  sum(Sales) as totalSales
from MyOrders
where CustomerID in (
  select id
  from customers
  where country = 'Germany'
)
group by CustomerID;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Show all customer details and find total orders made by each customer.

A
select
  c.*,
  o.totalOrders
from customers c
left join (
  select CustomerID,
  count(*) as totalOrders
  from MyOrders
  group by CustomerID
) o
on c.id = o.CustomerID
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When to prefer row subquery over join:

A

Ask yourself if you need value from another column of the right table.

single column -> go for row subquery
multi column -> go for join

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Find all orders with sales that are higher than the global average sales.

A
select
  OrderID,
  Sales,
  avgSales
from
(select
  OrderID,
  Sales,
  avg(Sales) over() as avgSales
from MyOrders)t
where sales > avgSales;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Find all orders with sales that are higher than the global average sales,
using scalar subquery.

Hint: this time, try using subquery with comparison operator in where clause

A
select
  OrderID,
  Sales
from MyOrders
where sales > (select avg(Sales) from MyOrders);
17
Q

Find female employees whose salaries are greater than salaries of at least one (any) male employee.

Employees table: EmployeeId, FirstName, Salary, Gender

A
select EmployeeId,
FirstName,
Salary
where Gender = 'F'
and Salary > ANY (
select salary from Employees where Gender = 'M'
)
18
Q

The unique feature of “correlated subquery” is that
it gets executed for each _ returned by main query.

A

row

Think of it like streaming execution (map operation)

19
Q

Show all customer details and total orders of each customer.

A
select *,
  (select count(Sales) from MyOrders o where o.CustomerID = c.id) totalOrdersPerCustomer
from customers c;

correlation helps in making the subquery scalar!

20
Q

Show the details of orders made by customers in Germany.

Note: Orders table has orderID and customerID.
Countries table has cutomerID and country.

A
select orderId, customerId
from Orders o
where exists (
select 1
from Countries
where country = 'Germany' and customerId = o.customerId
)

exists is more efficient than in