Professional Documents
Culture Documents
Even though it’s pretty long, it’s not that complicated. According to this
reply, advanced SQL covers selecting columns, aggregate functions
like MIN() and MAX(), the CASE WHEN statement, JOINs,
the WHERE clause, GROUP BY, declaring variables, and subqueries.
It’s interesting to note that sometimes JOINs are considered advanced
while writing stored procedures is still regarded as basic knowledge. I can
understand why because one user hints at the problem with JOINs. Even
though they are generally considered basic knowledge, many SQL users
learn much more advanced topics before really understanding JOINs. This
is how the basics easily become advanced knowledge. It’s not unusual to
find someone using flashy functions, triggers, and whatnot – without
knowing how to write a simple JOIN.
ADVANCED SQL:
Before explaining what advanced SQL is, it’s essential to know what it
isn't. When you look at our courses and articles, basic/intermediate SQL is
anything.
Basic:
1. SELECT ing columns from a table
2. Aggregates Part 1: COUNT, SUM, MAX/MIN
3. Aggregates Part 2: DISTINCT, GROUP BY, HAVING
Intermediate:
4. JOINs, ANSI-89 and ANSI-92 syntax
5. UNION vs UNION ALL
6. NULL handling: COALESCE & Native NULL handling
7. Subqueries: IN, EXISTS, and inline views
8. Subqueries: Correlated
9. WITH syntax: Subquery Factoring/CTE
10. Views
Advanced Concept:
You must be familiar with these topics if you claim to know SQL. These
are things you should understand before moving to more advanced topics.
Window functions
Common Table Expressions (CTEs)
GROUP BY extensions (ROLLUP, CUBE, and GROUPING SETS)
Window functions:
SQL window functions allow you to perform operations that are often
required for creating reports, e.g. ranking data, calculating running totals
and moving averages, finding the difference between rows, etc. Not only
that, but you can also divide data into windows, which enables you to
perform operations on data subsets rather than the data as a whole.
Let’s see an example. This code will show the difference in yearly numbers
of cars sold, according to make
Code:
SELECT car_make,
cars_sold,
year,
cars_sold - LAG(cars_sold) OVER
(PARTITION BY car_make ORDER BY year) AS sales_diff
FROM cars_sale;
To get this information, you first have to select the columns you want in the
result: car_make, cars_sold, year. To get the yearly difference, subtract the
previous year’s sale from the current year’s sale: cars_sold -
LAG(cars_sold) OVER (PARTITION BY car_make ORDER BY year) AS sales_diff.
Here, cars_sold means the current year’s sales. The LAG() function allows
you to fetch data from the previous row. The OVER clause signifies this is a
window function. Then follows the PARTITION BY clause, which is used to
define the window (data subset) we want to use. In this case, it’s
the car_make; this means the function will calculate the sale difference only
within a specific car make. When it runs into another car make, the function
will reset and start calculating the sales difference all over again.
car_mak cars_sol
year sales_diff
e d
201
Nissan 459,663 NULL
5
201
Nissan 312,453 -147,210
6
201
Nissan 541,223 228,770
7
201
Nissan 452,844 -88,379
8
201
Nissan 584,256 131,412
9
201
Renault 1,342,558 NULL
5
16,842,55 201
Renault -408,904
2 7
-
201
Renault 1,425,895 15,416,65
8
7
201
Renault 1,548,698 122,803
9
GROUP BY Extensions:
ROLLUP
CUBE
GROUPING SETS
Unlike regular GROUP BY, ROLLUP lets you group the data into multiple data
sets and aggregate results on different levels. Fancy talk, but simply put:
you can use ROLLUP to calculate totals and subtotals, just like in Excel pivot
tables.
Finally, there are GROUPING SETs. A grouping set is a set of columns you use
in the GROUP BY clause. You can connect different queries containing GROUP
BY if you use UNION ALL. However, the more queries you have, the messier it
gets. You can achieve the same result but with much neater queries by
using GROUPING SETS.
Code:
SELECT manufacturer,
model,
store,
SUM(quantity) AS quantity_sum
FROM guitars
GROUP BY ROLLUP (manufacturer, model, store)
ORDER BY manufacturer;