SQL Flashcards
What does SQL stand for?
Structured Query Language
LIKE operator syntax
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”)
Brackets order of operation
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
Grouping data in SQL
Columns in the SELECT statement either need an aggregation function applied to them OR need to be included in the GROUP BY statement
CASE WHEN structure
Allows you to create custom columns based on conditions. You can group your data based on a custom column created within the same query
HAVING VS WHERE
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 to explore a table quickly (syntax)
SELECT * FROM new_table LIMIT 5;
Query order of operations
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
What is a primary key?
A column that is the unique identifier of a specific table. One value of the column represents one unique row in the table
What is a foreign key?
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
When do we get duplicated rows?
When both join keys used in the JOIN are not unique identifiers of the tables
Types of JOIN
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
JOIN syntax
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>
Do join keys need to be the same between joins?
No
Multiple join syntax with multiple join keys
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>
What does UNION allow to do?
UNION allows us to write two queries, which get combined into a final query result
Only requirement for UNION to work
The alignment of the number of columns and the data types of each column between the two queries