DB SQL Interview Questions Flashcards

Enhance Knowledge of SQL for interviews

1
Q

What is a database

A

A database is an organized collection of data.

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

Join (inner vs left)

A

Inner gets the intersection of both tables, left gets that plus all of table a.

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

Aggregation Grouping using SUM

A

SELECT department, SUM(sales) AS “Total sales” FROM order_details GROUP BY department;

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

Aggregation Grouping using MAX

A

SELECT working_area, MAX(commission)
FROM agents
GROUP BY working_area;

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

Aggregation Grouping using MAX with Order By

A
SELECT cust_city, cust_country, 
MAX(outstanding_amt) 
FROM customer 
GROUP BY cust_country, cust_city 
ORDER BY cust_city;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What a “having” statement does, and when to use it

A
Having was added to allow for aggregate functions:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
e.g.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Coalesce (what it does and when you’d use it)

A

Returns first non null value in list. Use when you need the first element that is available in list of possible null values.

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

What is an analytic function

A

Computes values over a group of rows. Returns 1 value per row.

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

What is an aggregate function

A

Computes values over a group of rows. Returns 1 value per group.

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

Example of an analytic function

A

PARTITION BY: divides rows into partitions
e.g.
SELECT firstname, department, startdate,
RANK() OVER ( PARTITION BY department ORDER BY startdate ) AS rank
FROM Employees;

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

Example of an aggregate function

A

COUNT, MIN and MAX are examples of aggregate functions.

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

What is the difference between UNION and UNION ALL

A

UNION removes duplicates, UNION ALL does not.

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

Give an example of a subquery

A
SELECT column-names
  FROM table-name1
 WHERE value IN (SELECT column-name
                   FROM table-name2 
                  WHERE condition)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly