A collection of fragments of understanding in the pursuit of deeper questions.
SQL Sub Languages SQL consists of many types of statements, that can be grouped into 4 sublanguages:
Data Definition Language (DDL)
![]() |
![]() |
![]() |
|---|
Data Manipulation Language (DML)
![]() |
![]() |
![]() |
![]() |
|---|
Data Query Language (DQL) - The Basics
![]() |
![]() |
![]() |
|---|
SELECT: Calculations & Aliases Calculations are scalar, returning one value per row.
Using scalar expressions in the SELECT clause:
SELECT: Functions In the select list (but also in the WHERE clause) we can use built-in functions:
![]() |
![]() |
|---|
SELECT DISTINCT & ORDER BY
SQL query results are not truly relational:
![]() |
![]() |
|---|
SELECT & WHERE The WHERE clause limits the rows returned by a SELECT query. The WHERE keyword is followed by a logical predicate. In the predicate we can use:
SELECT and CASE The CASE statement has the functionality of an IF-THEN-ELSE statement.
![]() |
![]() |
|---|
UNION and UNION ALL The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. The UNION ALL operator behaves like UNION but it doesn't remove the duplicates.
Data Query Language (DQL) - Understanding Joins Definition: Cartesian product Characteristics of a Cartesian product
JOINS A JOIN operation puts together the columns of two tables, by matching the rows based on a predicate.
CROSS JOIN
INNER JOIN
LEFT OUTER JOIN
OUTER JOINS
Handling NULLs
Grouping and Summarizing Data Aggregate Functions
Aggregate Functions
Group By GROUP BY creates groups for output rows, according to a unique combination of values specified in the GROUP BY clause. The WHERE clause filters the rows before the aggregation is applied.\ We use GROUP BY + an aggregate function every time we want to compute an aggregation for each of the groups. Detail rows are "lost" after the GROUP BY clause is processed.
How GROUP BY works
Filtering Aggregates HAVING clause provides a search condition that each group must satisfy. HAVING clause is processed after GROUP BY.
COUNT DISTINCT How many different customers bought placed an order?
SubQueries Subqueries are nested queries: queries within queries. Results of inner query passed to outer query.
EXISTS and NOT EXISTS When a subquery is used with the keyword EXISTS, it works only as an existence test.
EXISTS evaluates to TRUE or FALSE
Syntax:
Customer with at least 1 order:
Customer with No orders:
Analytical Functions Set of SQL functions for analytical tasks. Analytical functions make many SQL tasks easy. Some functions:
ROW_NUMBER ROW_NUMBER provides a simple way to get row numbering in a result sets. The row numbers can then be used for many purposes. Syntax: ROW_NUMBER() OVER(PARTITION BY col1, col2, ... ORDER BY col1, col2, ...)
Example: find the largest order for each customer.
The OVER clause The OVER clause consists of 2 parts:
LAG & LEAD LAG returns a value taken from n rows before the current row. (n is a parameter). If no value is found, the function returns the specified default value. Syntax: LAG(column_name, n, default_value) OVER(PARTITION BY partition_by_cols ORDER BY order_by_cols). LEAD returns a value taken from n rows past the current row. (n is a parameter). If no value is found, the function returns the specified default value. Syntax: LEAD(column_name, n, default_value) OVER(PARTITION BY partition_by_cols ORDER BY order_by_cols).
LAG Example
Aggregation Function with the OVER Clause The OVER clause allows computing the aggregations for a partition (or for all the rows in the result set). With the OVER clause the aggregations are returned along with each single row.
![]() |
![]() |
|---|
Windowing In addition to PARTITION BY and ORDER BY the OVER clause allows us to specify a window of rows to which the aggregation function is applied. Keywords:
Windowing Examples
![]() |
![]() |
|---|