SQL Syntax Flashcards

(47 cards)

1
Q

What does the SELECT statement do in SQL?

A

It specifies which columns to display in the result set.

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

What clause specifies which table to retrieve data from?

A

FROM clause.

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

Which clause filters rows based on conditions?

A

WHERE clause.

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

How do you sort query results?

A

Using ORDER BY followed by column name(s) and ASC or DESC.

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

What is the purpose of GROUP BY?

A

To aggregate data across rows that share a common value in one or more columns.

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

What is the difference between WHERE and HAVING?

A

WHERE filters rows before aggregation; HAVING filters groups after aggregation.

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

What does COUNT(*) do?

A

Counts all rows in the result set including duplicates and NULLs.

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

How do you count distinct values in a column?

A

COUNT(DISTINCT column_name).

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

Which SQL function returns the average of a numeric column?

A

AVG().

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

Which function returns the sum of all values in a column?

A

SUM().

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

Which function returns the largest value in a column?

A

MAX().

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

Which function returns the smallest value in a column?

A

MIN().

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

What keyword removes duplicate rows from a query result?

A

DISTINCT.

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

What is the purpose of the AS keyword?

A

To rename a column or table with an alias.

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

What are the main types of JOINs in SQL?

A

INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN

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

What does an INNER JOIN return?

A

Rows with matching values in both tables.

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

What does a LEFT JOIN return?

A

All rows from the left table and matched rows from the right; unmatched right values become NULL.

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

What does a RIGHT JOIN return?

A

All rows from the right table and matched rows from the left; unmatched left values become NULL.

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

What does a FULL OUTER JOIN return?

A

All rows when there is a match in either left or right table

20
Q

How do you combine results of two queries?

A

Using UNION or UNION ALL.

21
Q

What is the difference between UNION and UNION ALL?

A

UNION removes duplicates

22
Q

What is a subquery?

A

A query nested inside another query.

23
Q

What does the IN operator do?

A

Tests whether a value matches any value in a list or subquery.

24
Q

What does BETWEEN do?

A

Filters values within a given range (inclusive).

25
What does LIKE do?
Performs pattern matching using % (wildcard for any characters) or _ (single character).
26
How do you replace NULL with a default value?
COALESCE(column, default_value)
27
What is a CASE statement?
Conditional logic in SQL for returning different values based on conditions.
28
What is the purpose of LIMIT (or TOP in SQL Server)?
Restricts the number of rows returned.
29
What are window functions?
Functions that perform calculations across sets of rows related to the current row without collapsing them.
30
What does ROW_NUMBER() do?
Assigns a unique sequential integer to rows within a partition.
31
What does RANK() do?
Gives ranks with gaps for ties in ordered results.
32
What does DENSE_RANK() do?
Gives ranks without gaps for ties.
33
What clause defines partitions for window functions?
PARTITION BY.
34
What clause defines order for window functions?
ORDER BY inside the OVER() clause.
35
What is the difference between GROUP BY and window functions?
GROUP BY collapses rows into one per group
36
What is a primary key?
A column (or set of columns) that uniquely identifies each row in a table.
37
What is a foreign key?
A column that links to the primary key in another table to create a relationship.
38
What does SELECT DISTINCT city FROM customers do?
Returns a list of unique city names from the customers table.
39
How can you find the total sales per customer?
SELECT customer_id, SUM(amount) FROM sales GROUP BY customer_id
40
How can you filter groups (e.g. customers) with total sales > 1000
Use HAVING SUM(amount) > 1000. SELECT customer, SUM(amount) AS total_sales FROM sales GROUP BY customer HAVING SUM(amount) > 1000;
41
What is the execution order of SQL clauses?
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.
42
How do you calculate cumulative sums in SQL?
Using SUM(amount) OVER(ORDER BY date).
43
How do you find the previous row’s value?
Using LAG(column) OVER(ORDER BY some_column).
44
How do you find the next row’s value based on some_column ordering?
Using LEAD(column) OVER(ORDER BY some_column).
45
How can you count the number of unique users per day?
SELECT date, COUNT(DISTINCT user_id) FROM table GROUP BY date.
46
What is a common table expression (CTE)?
A temporary result set defined using WITH that can be referenced within the main query.
47
What is the difference between CTE and subquery?
CTEs can be referenced multiple times and improve readability; subqueries cannot.