Common Relational DB SQL concepts for quick revisit, recapped mainly from w3school and go/Bard, not every DB supports the listed syntax, so please use them accordingly.
SQL Clauses Layout Order
1 | SELECT column_name(s) |
Distinct
1 | SELECT COUNT(DISTINCT Name) FROM Customers; |
Operators in Where
WHERE
operators:
- -, >, <, >=, <=, !=(<>)
- BETWEEN
- LIKE
- IN
Quotes for Text
1 | -- Single quote for text value |
Using Parenthesis
1 | SELECT * FROM Customers |
Min and Max
1 | SELECT MAX(Price) AS LargestPrice |
Count, Avg and Sum
1 | SELECT COUNT(ProductID) |
Order By
1 | SELECT * FROM Customers |
NULL
Not every DB supports NULL for empty values, be aware.
1 | SELECT CustomerName, ContactName, Address |
Limit
1 | SELECT * FROM Customers |
In
The IN
operator is a shorthand for multiple OR conditions.
1 | SELECT * FROM Customers |
Alias
1 | -- New column with a Literal value |
Joins
- (INNER) JOIN: Returns records that have matching values in all tables.
- LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table.
- RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table.
- FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table.
See explanation diagrams here.
Inner Join
1 | SELECT column_name(s) |
You can use USING
to subsitute ON
if the columns name are the same, please
see here
1 | SELECT column_name(s) |
Left Join
1 | -- Show both CustomerID can better explain the left join |
The LEFT JOIN keyword returns all records from the left table (Customers), even
if there are no matches in the right table (Orders) and if there is no that
CustomerID in Orders table, you get null
for the Orders selected fields.
Right Join
1 | SELECT |
The RIGHT JOIN keyword returns all records from the right table (Employees), even if there are no matches in the left table (Orders).
Full Join
1 | SELECT Customers.CustomerName, Orders.OrderID |
The FULL OUTER JOIN keyword returns all matching records from both tables whether the other table matches or not. So, if there are rows in “Customers” that do not have matches in “Orders”, or if there are rows in “Orders” that do not have matches in “Customers”, those rows will be listed as well.
Self Join
1 | -- there will be duplicates introduced by switch CustomerName in A and B tables |
Union
The UNION operator is used to combine the result-set of two or more SELECT statements.
- Every SELECT statement within UNION must have the same number of columns.
- The columns must also have similar data types.
- The columns in every SELECT statement must also be in the same order.
1 | -- 'Customer' and 'Supplier' are literal value under the 'Type' column |
Group By
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
1 | SELECT column_name(s) |
Having
The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
1 | SELECT column_name(s) |
Exist
The EXISTS operator is used to test for the existence of any record in a subquery.
1 | SELECT column_name(s) |
Any and ALL
The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values.
1 | SELECT column_name(s) |
Subquery
This is how you can boost your query, basically you can use subquery in below clauses:
- SELECT
- FROM
- WHERE
- HAVING
- JOIN
Examples:
1 | -- SELECT clause |