SQL and databases

Stage 2 of 9 · 8 lessons · ~3 weeks

Querying one table

Write SELECT statements against one table: choose columns, filter rows, handle NULL, sort, limit and shape results with expressions and functions.

Start: SELECT, FROM, aliases and DISTINCT

Lessons· one lesson for each topic, in this order

  1. 1SELECT, FROM, aliases and DISTINCT
  2. 2Filtering rows with WHERE
  3. 3NULL and three-valued logic
  4. 4Sorting with ORDER BY
  5. 5Limiting rows: LIMIT, TOP and OFFSET FETCH
  6. 6CASE expressions
  7. 7Text and date functions in your dialect
  8. 8The logical order a query runs in

Do this

  • Write queries that select named columns with aliases and DISTINCT.
  • Filter with IN, BETWEEN, LIKE and bracketed AND and OR, predicting the row count first.
  • Find rows with NULL values and replace NULL with COALESCE.
  • Write a paginated query with ORDER BY and a tie-breaker in your dialect.
  • Use CASE to label rows and date functions to filter by the last 30 days.
  • Solve the first two sections of an online SQL practice set.

Start with· follow one of these

  • CoursePaid
    Grokking SQL for Tech Interviews

    Design Gurus · Intermediate · Subscription

    The basics, order of execution and functions chapters, each followed by graded problems

  • CourseFree
    SQLBolt

    SQLBolt · Beginner

    Free: Lessons 1 to 4, 8, 9 and 12 cover SELECT, filtering, sorting, NULLs, expressions and query order

Practise

Keep handy

  • The T-SQL SELECT, TOP and ORDER BY pages and the date and time functions list

  • Its SQL pages give quick examples and note where MySQL and SQL Server syntax differ

Also worth your time

Before moving on: A file of 30 working single-table queries against your sample database, each with a comment stating the question it answers, including NULL handling, CASE, date filters and a paginated query.

Tauseef's advice
Before you run a query, guess how many rows it will return. The habit of predicting and then checking catches more bugs than any tool, and it is exactly what you need later when joins start multiplying rows.

Mark this stage as not done?