5 - Queries with SQL (1/4) - Information Systems for Engineers - ETH Zurich - Fall 2026
Overview
Ghislain Fourny introduces practical SQL querying in PostgreSQL by mapping relational-algebra operations to SQL clauses: SELECT projects columns, WHERE filters rows, and AS renames output attributes. He explains default bag semantics and DISTINCT, query plans, SQL expressions and three-valued logic for NULL, then applies these ideas in a query-identification exercise and demonstrates date-casting syntax.
Key takeaways
- In PostgreSQL, SELECT chooses output columns and WHERE filters rows, so projection and selection correspond to different clauses.
- PostgreSQL uses bag semantics by default: duplicate rows remain unless SELECT DISTINCT is specified.
- DISTINCT applies to whole output rows, while PostgreSQL’s DISTINCT ON can deduplicate by selected columns but may choose an arbitrary representative row.
- SQL comparisons with NULL yield UNKNOWN; use IS NULL to test for missing values and IS DISTINCT FROM for null-aware comparisons.
- PostgreSQL builds and optimizes a query plan from SQL operations, which can be understood as a tree of relational-algebra steps.
- Date literals can be typed with DATE syntax, the ::date cast operator, or CAST(... AS date), with CAST supporting nested expressions.
Chapters
- SQL is the standard query language across relational-table products, and its compact syntax can complement tools such as ChatGPT, Claude, Codex, and Copilot.
- The lecture shifts from data definition and record updates to querying, building on relational-algebra operations including projection, selection, grouping, sorting, and joins.
- SQL is declarative and set-based: users specify the desired result while PostgreSQL handles the execution.
- SELECT * FROM persons returns every column and is a useful first query for inspecting an unfamiliar table.
- SELECT first_name, birth_date FROM persons performs relational-algebra projection by returning only the specified columns.
- PostgreSQL retains duplicate result rows by default, equivalent to SELECT ALL; SELECT DISTINCT removes duplicate complete rows.
- PostgreSQL’s DISTINCT ON can deduplicate by selected columns, such as birth_date, but may choose an arbitrary row when several people share a date.
- PostgreSQL parses and analyzes a query, then represents its operations in a query plan that can be understood as a tree of relational-algebra steps.
- A projection query can be represented as applying π to a source table, with the projected result at the root of the computer-science tree.
- The database can optimize the plan before executing it; most SQL users can write queries without inspecting plans, but advanced users can examine them.
- Use AS in the SELECT list to rename output columns, for example, selecting first_name AS who and birth_date AS when.
- Avoid assigning the same output name to multiple columns: PostgreSQL may permit it, but the resulting names can be ambiguous or unpredictable.
- Use WHERE for relational-algebra selection, such as filtering persons where last_name = 'Crusher'; SELECT performs projection, not row filtering.
- A SQL query contains clauses such as SELECT, FROM, and WHERE, while a filter condition such as last_name = 'Crusher' is a predicate.
- WHERE predicates can combine expressions using parentheses, AND, OR, NOT, and comparison operators such as <, >, =, and <>.
- SQL uses TRUE, FALSE, and UNKNOWN; comparisons involving NULL generally evaluate to UNKNOWN, which changes how compound conditions behave.
- Test missing values with IS NULL or IS NOT NULL, not = NULL, which evaluates to UNKNOWN rather than testing for a null.
- IS DISTINCT FROM and IS NOT DISTINCT FROM compare values while treating NULL as a comparable value instead of producing UNKNOWN.
- SQL arithmetic includes +, -, *, and /; the percent sign computes modulo, and PostgreSQL also provides exponentiation.
- PostgreSQL offers additional operators such as factorial and absolute value, plus functions including sine, cosine, and tangent.
- The lecture notes that PostgreSQL documentation is available during the exam, so students should know how to find less familiar operators and functions rather than memorize every one.
- Bitwise operations are available for advanced use, but Fourny says they are outside the course’s exam requirements.
- Students compare one input table, one output table, and four candidate queries to determine which SQL transformations produced the result.
- The correct candidate renames B as E, filters on B < C and E = D, and projects away C.
- Rows where B < C evaluates to UNKNOWN do not pass the WHERE filter, while a row with mismatched E and D values is also excluded.
- The exercise combines selection, projection, renaming, and NULL-aware predicate reasoning in one query.
- PostgreSQL can create a date value with DATE '2025-10-17', the cast operator '2025-10-17'::date, or CAST('2025-10-17' AS date).
- CAST can wrap a more complex expression, illustrating how SQL expressions can be nested and composed.
- SQL keywords such as SELECT, FROM, and WHERE are case-insensitive; uppercase keywords are a readability convention with historical roots.
- The lecture closes by directing students to the SQL exercises and TA sessions as they move from creating and populating tables to querying them.
Transcript excerpt
0:00 Welcome back everybody. So, now we have again a practical lecture. So, I think it's probably where the fun actually starts now because um this is where we really get to practically querying tables in SQL interactively in the notebook and this is the part where you can really yourself >> [snorts] >> uh try out things and and experiments and make yourself familiar with the SQL language. Super important, the SQL language is absolutely everywhere. Uh it's it's very very widespread. All products uh dealing with relational tables implement it. It's undisputed. There's not even any competitor to SQL. This is the language for querying tables. So, having the expertise to
0:47 write SQL is very useful even in the age of LLMs. Uh uh the fact that SQL is so compact and and and uh text makes it very easy to an LLM, uh Claude, Codex, ChatGPT, Copilot to to output SQL. Uh so, it's also very complementary to to uh um to agents or generative AI. All right. In fact, it costs less tokens to use SQL than to do the same thing with Python, for example. >> [snorts] >> All right. So, where are we? We saw data definition, data definition language, uh what to do at the level of the table, the columns, the types, uh domains and so on, primary keys, foreign keys, constraints and so on.
1:35 We already covered how to create tables in SQL, how to drop tables even including the trick with if exists in order not to get an error if it's already there. Uh very useful for initialization scripts. We can rename tables, we can add columns, we can remove columns, we can rename columns. Uh we can do all of that um >> [clears throat] >> in the uh in the in SQL. And that we've
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Ghislain Fourny's lectures.