ZRH --:--:--

SQL execution order and exercises

1 min read2 sections
On this page

✅ SQL Logical Execution Order (most important version)

  1. FROM

  2. ON

  3. JOIN

  4. WHERE

  5. GROUP BY

  6. HAVING

  7. SELECT

  8. DISTINCT

  9. WINDOW functions (e.g., LAG/LEAD)

  10. ORDER BY

  11. LIMIT / OFFSET

Ex 1

Visits
Column NameType
visit_id customer_idint int

visit_id is the column with unique values for this table. This table contains information about the customers who visited the mall.

Transactions
Column NameType
transaction_id visit_id amountint int int

transaction_id is column with unique values for this table. This table contains information about the transactions made during the visit_id.

Write a solution to find the IDs of the users who visited without making any transactions and the number of times they made these types of visits.

Return the result table sorted in any order.

The result format is in the following example.

Show code (15 lines)
# Write your MySQL query statement below
SELECT
  customer_id,
  COUNT(visit_id) AS count_no_trans
FROM
  Visits
WHERE
  visit_id NOT IN (
    SELECT
      visit_id
    FROM
      Transactions
  )
GROUP BY
  customer_id

Ex 2

Weather
Column NameType
id recordDate temperatureint date int

id is the column with unique values for this table. There are no different rows with the same recordDate. This table contains information about the temperature on a certain day.

Write a solution to find all dates’ id with higher temperatures compared to its previous dates (yesterday).

# Write your MySQL query statement below
SELECT id
FROM
    (SELECT
        id,
        temperature,
        recordDate,
        lag(temperature) OVER(ORDER BY recordDate) AS prev_temp
FROM Weather) t
WHERE temperature > prev_temp

Back to top

Esc

Type at least two letters. Search covers every project and knowledge-base note.

oz@zrh: ~
QUICK DOSSIER · 30 SEC READ

OLIVER ZINGG

Psychologist and data scientist in Zürich. I design the study, model the data and explain what it means.

STATUS
Open to full-time data science roles from January 2027
NOW
Working student, data science & project management at Zurich Insurance (since Nov 2025)
THESIS
M.Sc. thesis (HSLU): leveraging double machine learning for robust decision-making, due December 2026
EDUCATION
M.Sc. Psychology (UZH, 5.6) · M.Sc. Applied Information & Data Science (HSLU, Dec 2026)
TOOLS
R, Python, SQL, Matlab · Power BI, Tableau · mixed models, ML, BERTopic

TOP CASE FILES

  1. Learning and memory formation in children and adolescents with psychiatric disordersMaster's thesis, UZH · 2023
  2. Support vector machines vs. CNNs for Alzheimer's disease classificationBachelor's thesis, UZH · 2021
  3. Simpson's paradox and mixed-effects modelsStatistics · 2023