SQL Flashcards

1
Q

What does SQL stand for?

A

Structured Query Language

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

LIKE operator syntax

A

Used to search for a specified pattern in a column. % represents zero, one, or multiple characters. _ represents a single character.
(SELECT * FROM employees WHERE name LIKE ‘Joh%’; - retrieves all employees whose names start with “Joh”)

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

Brackets order of operation

A

Brackets prioritise everything within the brackets over the code outside the brackets. This means the combination of filters within the brackets will run first and the remaining filter conditions will run after

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

Grouping data in SQL

A

Columns in the SELECT statement either need an aggregation function applied to them OR need to be included in the GROUP BY statement

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

CASE WHEN structure

A

Allows you to create custom columns based on conditions. You can group your data based on a custom column created within the same query

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

HAVING VS WHERE

A

Only apply HAVING if you need to filter aggregated data, otherwise use WHERE.
GROUP BY always gets executed before HAVING. Because of that, we can only filter in HAVING using columns available in GROUP BY

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

How to explore a table quickly (syntax)

A

SELECT * FROM new_table LIMIT 5;

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

Query order of operations

A

FROM - Get data from the table(s)
WHERE - Filter rows
GROUP BY - Group the data
HAVING - Filter grouped data
ORDER BY - Order the data
SELECT - Select columns
LIMIT - Limit the number of rows

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

What is a primary key?

A

A column that is the unique identifier of a specific table. One value of the column represents one unique row in the table

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

What is a foreign key?

A

A column that is not the unique identifier of the table it is stored in, but it is a unique identifier of another table. A foreign key value can appear more than once in the table

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

When do we get duplicated rows?

A

When both join keys used in the JOIN are not unique identifiers of the tables

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

Types of JOIN

A

INNER JOIN - only includes rows where the join keys match
LEFT JOIN - includes all rows of the “left” table and the matches from the right table
RIGHT JOIN - includes all rows of the “right” table and the matches from the left table
FULL JOIN - includes all rows from both tables even if there are no matches in the join key

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

JOIN syntax

A

SELECT
<table_1.column_1> ,
<table_1.column_2>
FROM
<table_1>
_ JOIN -- _ can be replaced with INNER, or any of the other JOIN types (LEFT, RIGHT, FULL)
<table_2> ON <table_1.join_key> = <table_2.join_key>
WHERE …;</table_2.join_key></table_1.join_key></table_2></table_1></table_1.column_2></table_1.column_1>

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

Do join keys need to be the same between joins?

A

No

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

Multiple join syntax with multiple join keys

A

SELECT
<table_1.column_1> ,
<table_1.column_2>
FROM
<table_1>
_ JOIN -- _ can be replaced with INNER, or any of the other JOIN types (LEFT, RIGHT, FULL)
<table_2>
ON
<table_1.join_key_1> = <table_2.join_key_1>
AND -- the AND keyword let us use JOIN tables using more than one join key
<table_1.join_key_2> = <table_2.join_key_2></table_2.join_key_2></table_1.join_key_2></table_2.join_key_1></table_1.join_key_1></table_2></table_1></table_1.column_2></table_1.column_1>

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

What does UNION allow to do?

A

UNION allows us to write two queries, which get combined into a final query result

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

Only requirement for UNION to work

A

The alignment of the number of columns and the data types of each column between the two queries

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

UNION ALL VS UNION DISTINCT

A

UNION ALL shows all rows from the two queries combined, whereas UNION DISTINCT only shows unique rows

19
Q

COALESCE()

A

Allows us to fill NULL values in a column (works with any data type):
– COALESCE(<column>, value if <column> is NULL)
SELECT COALESCE(country, 'Other') AS country,
sales_team
FROM meta_clients;
You can actually use another column to fill populate the NULL value as well:
SELECT COALESCE(country, sales_team) AS country,
sales_team
FROM meta_clients;
You can actually use more than one possible fill value - if the first two columns have a NULL value, the third one will populate the column:
SELECT COALESCE(country, sales_team, 'Other') AS country,
sales_team
FROM meta_clients;</column></column>

20
Q

LOWER() and UPPER()

A

To handle capitalisation in text

21
Q

TRIM()

A

Removes all whitespace before and after the text in a cell

22
Q

REPLACE()

A

To change or remove unwanted text from a string column:
SELECT region,
REPLACE(region, ‘.’, ‘’) AS cleaned_region
FROM meta_clients;

23
Q

SUBSTRING()

A

Used to extract a substring from a string:
SELECT SUBSTRING(‘String expression’, start position, length) AS extracted_string;

24
Q

REGEXP_LIKE

A

Checks if a string matches a regular expression pattern:
SELECT * FROM meta_clients WHERE REGEXP_LIKE(industry, ‘Retail|Consumer Goods’, ‘i’); – use ‘i’ for case insensitive, which will search for all the statements that are presenting the same words within the specified column instead of having to specify all the capitals/lower case, etc.

25
Q

REGEXP_REPLACE

A

Replaces substrings in a string based on a regular expression pattern:
SELECT client_id,REGEXP_REPLACE(client_id, ‘[0-9_]’, ‘’, ‘g’)
– g for global, which means all the patterns identified will be replaced

26
Q

CAST()

A

Used to convert an expression from one data type to another:
SELECT CAST(‘123’ AS INT);
You can also cast like this: ::numeric
Syntax: SELECT expression::new_data_type AS new_column_name
FROM your_table;

27
Q

ROUND()

A

Used to round a numeric value to a specified number of decimal places:
Syntax: ROUND(number, decimals)
ROUND(revenue, 2)

28
Q

EXTRACT()

A

To extract individual components from dates or timestamps:
EXTRACT(YEAR FROM last_updated_date) AS year
EXTRACT(MONTH FROM last_updated_date) AS month
EXTRACT(DAY FROM last_updated_date) AS day

29
Q

Filtering dates using EXTRACT()

A

Very effective because it removes the need to use the entire date format:
WHERE EXTRACT(MONTH FROM last_updated_date) = 5
AND EXTRACT(YEAR FROM last_updated_date) = 2023

30
Q

TO_DATE()

A

Used to convert a string representing a date into a date data type:
SELECT TO_DATE(‘2023-01-01’, ‘YYYY-MM-DD’) AS custom_date;

31
Q

CURRENT_DATE()

A

Returns the current date as a date data type:
SELECT CURRENT_DATE AS today;

32
Q

Why are WITH statements so useful?

A

Allows you to write a series of simple SQL queries by defining CTE (Common Table Expressions) to solve more complex problems. Simple blocks of code for easy troubleshooting

33
Q

WITH statement syntax

A

WITH my_new_table AS ( – we give the CTE the alias “my_new_table”

SELECT * FROM table

)

SELECT * FROM my_new_table; – we reference the CTE we created using WITH in our final query

Between multiple CTEs any time another table is coming, you must include a comma. Only after your final table you will not use a comma

34
Q

Subqueries

A

Created by nesting a SELECT … FROM query within brackets - everything inside the brackets gets executed first

35
Q

Window functions

A

Allow you to apply functions over a set of rows relative to the current row in a table

36
Q

GROUP BY VS Window functions

A

The aggregation function together with GROUP BY will aggregate all the rows based on the GROUP BY. The outcome of this is an aggregated table with fewer rows than the original table.
A window function applies OVER the entire table (e.g. ROW_NUMBER) and the query result will contain the same number of rows as the original table.

37
Q

Most used window functions:
ROW_NUMBER()

A

Assigns a unique integer to rows within a partition of a result set, starting at 1 for the first row in each partition.
ROW_NUMBER() OVER (ORDER BY created_at DESC) AS row_number

38
Q

Most used window functions:
LAG(column_name)

A

Accesses data from a previous row in the same result set
LAG(created_at) OVER (ORDER BY created_at ASC) AS previous_value

39
Q

Most used window functions:
LEAD(column_name)

A

Accesses data from a subsequent row in the same result set

40
Q

Most used window functions:
FIRST_VALUE(column_name)

A

Returns the first value in an ordered set of values

41
Q

Most used window functions:
LAST_VALUE(column_name)

A

Returns the last value in an ordered set of values

42
Q

PARTITION BY

A

Can be applied within the window function to reset the window function within a specified group. Allows you to run functions within a particular group and reset the calculation between groups:
ROW_NUMBER() OVER (PARTITION BY state ORDER BY num_customers DESC) AS rank

43
Q

ROLLUP

A

Can be applied within the GROUP BY to create total and subtotal lines in your query result:
SELECT state AS state,
age_group AS age_group,
COUNT(customer_id) AS num_customers
FROM customers
GROUP BY ROLLUP(state, age_group)
ORDER BY state;