Reporting (Querying) Flashcards

(18 cards)

1
Q

What is the art of querying and what is the standard structure for SQL queries?

A

Querying is the art of retrieving specific data from your database. Structure: SELECT… FROM… WHERE… GROUP BY… HAVING… ORDER BY… LIMIT…

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

What’s the key difference between the syntactic order and execution order of SQL clauses?

A

Syntactic order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT. Execution order: All other clauses execute first in syntactic order, then SELECT executes LAST after the table has been processed

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

Explain the conceptual process: why does SELECT execute last and what happens before it?

A

First we choose a table, then do whatever we need (filter rows, group, order, etc.) that affects the table as a whole. Finally, we inject the processed result into SELECT to retrieve specific columns, apply functions, etc.

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

What can the SELECT clause retrieve and provide examples of each type?

A

Can retrieve: 1. Columns (column_name), 2. Variables, 3. Expressions, 4. Aggregate functions (AVG(column_name), SUM(column_name)). Example: SELECT name, salary, AVG(salary) FROM employees;

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

What does DISTINCT do and write a query to get unique departments from an employees table.

A

DISTINCT removes duplicate values from results. Query: SELECT DISTINCT department FROM employees;

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

What is the purpose of AS and write a query renaming columns for better readability.

A

AS gives temporary names (aliases) to columns or values - like allocating to variables. Example: SELECT name AS employee_name, salary AS employee_salary FROM employees;

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

Why is the FROM clause essential even when the table name is obvious?

A

The RDBMS doesn’t automatically know where to get data from - FROM acts as a pointer to the data origin. Without it, the system can’t locate the specified tables.

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

When do functions execute in the query process and what do they evaluate?

A

Functions execute after the table is transformed by other clauses, then the result is input into SELECT. They evaluate based on the processed output at that stage and return values at their position.

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

What does WHERE do and write examples of the 4 main types of conditions it supports.

A

WHERE filters rows by keeping only those where conditions are true. Types: 1. Comparison (salary > 50000), 2. Logic (dept=’Sales’ AND salary>60000), 3. IN (dept IN (‘Sales’,’Marketing’)), 4. BETWEEN (salary BETWEEN 50000 AND 70000)

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

Write a WHERE clause that finds employees with no manager but who do have a department assigned.

A

WHERE manager_id IS NULL AND department_id IS NOT NULL

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

How should you think about GROUP BY and what happens to subsequent clauses?

A

Think of GROUP BY as separating the table into different sub-tables for each group. Any subsequent clause (including SELECT) is applied to each group individually.

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

When GROUP BY uses multiple columns, how are rows grouped?

A

Rows are grouped based on having similar values for ALL mentioned columns - they must match on every specified column to be in the same group.

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

What’s the key difference between HAVING and WHERE clauses?

A

WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY creates them - the condition must relate to the grouped columns or aggregate functions.

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

What happens when you use HAVING without GROUP BY?

A

It treats the entire table as one big group - essentially works like WHERE but is applied after other processing.

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

Write a query that orders employees by department ascending, then by salary descending.

A

SELECT * FROM employees ORDER BY department ASC, salary DESC;

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

How does ORDER BY work with multiple columns?

A

Rows are sorted by the first column, then if there are ties (similar values), those tied rows are sorted by the second column, and so on.

17
Q

Write a query to get employees 11-20 (skip first 10, get next 10) from a salary-ordered list.

A

SELECT * FROM employees ORDER BY salary DESC LIMIT 10 OFFSET 10; (or LIMIT 10, 10 in some SQL dialects)

18
Q

Write a query to find departments with average salary > 60000, showing department and avg salary, ordered by avg salary descending.

A

SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department HAVING AVG(salary) > 60000 ORDER BY avg_salary DESC;