Raw SQL: select & from
This course introduces SQL through SELECT and FROM in PostgreSQL. It explains relational tables, rows, columns and primary keys, and how a query moves from parser to planner to executor. Lessons cover SELECT as an expression evaluator, reading tables with FROM, choosing columns instead of SELECT *, aliases with AS, CASE expressions and NULL behavior with COALESCE and NULLIF. The last lesson connects these ideas to ActiveRecord, showing the SQL behind select, pluck and find_by_sql and how to read it in the Rails log.
What you'll learn
- ✓How relational databases store data in tables, rows and columns
- ✓Using SELECT with literals, arithmetic, string concatenation and built-in functions
- ✓Reading tables with FROM and selecting only the columns you need
- ✓Naming results with AS aliases and building CASE expressions
- ✓How NULL behaves in comparisons, expressions and aggregates
- ✓How ActiveRecord methods translate to SELECT and FROM queries
What is SQL?
Imagine a massive library with millions of books, but no librarian. The books are organized on shelves by category, author, and year. You can't just shout "give me something good!" You need to write a precise request on a slip of paper: which shelf, which section, which criteria. The library reads…
Read the full lesson free →Curriculum
7 lessonsWhat is SQL?
Explains relational databases, tables, rows, primary keys, SQL sub-languages and PostgreSQL's query processing pipeline.
Your first SELECT
Uses SELECT without a table to evaluate literals, arithmetic, string concatenation with || and built-in functions.
The FROM clause
Shows how FROM reads table pages, why row order is not guaranteed, and how table naming works.
Choosing columns
Covers the cost of SELECT *, table-qualified column names, expressions in the column list and select versus pluck.
Column aliases & expressions
Names computed columns with AS, builds arithmetic and CASE expressions, and explains where aliases can be used.
NULL: the billion-dollar mistake
Explains three-valued logic, IS NULL, NULL propagation, COALESCE, NULLIF and how aggregates treat NULL values.
SELECT & FROM in Rails
Connects ActiveRecord methods to generated SQL, reading the Rails log, find_by_sql, to_sql and explain.