liveBook · Manning

Database result set

As explained in Learn SQL in a Month of Lunches

A query result, or result set, is the tabular collection of rows returned by executing a database query.

A database result set is the collection of rows produced by a SELECT query. For example, selecting TitleName from the title table returns eight rows containing title names. Treat the result as the answer to a question asked of the database, rather than as a permanently ordered list. section 3.2.1

SELECT TitleName
FROM title;

This query returns one column from every qualifying row in title. Without an explicit ordering instruction, the displayed row order has no guarantee. section 3.2.1

When working with result sets, remember these practical rules:

  • Do not rely on row order unless the query explicitly orders its results; repeated executions can display rows differently. section 3.2.1
  • Set operators can evaluate two SELECT results as one result set, but the queries must return matching column counts and compatible data types. section 10.1
  • For combined results, column names and aliases come from the first SELECT, and ORDER BY belongs only after the final SELECT. section 10.1

Combining names with UNION

UNION joins the first and last names selected from customer and author into one result set, then orders that output by last name and first name. Identical rows are removed. Add a literal third column such as 'customer' or 'author' when the output must show where each row came from. section 10.2

SELECT FirstName, LastName
FROM customer
UNION
SELECT FirstName, LastName
FROM author
ORDER BY LastName, FirstName;

The two SELECT statements produce compatible two-column results; UNION turns them into one ordered result set. section 10.2

How Learn SQL in a Month of Lunches covers it

How deep it goes
Defines SELECT output as rows and details compatibility, naming, and ordering rules for combining results.
Examples
Uses title names and customer and author names, including filtered names and a source-label column.
What it assumes
Introduces result sets through simple SELECT queries while assuming familiarity with table and column names.
What you can do after
Evaluate/understand query output, avoid assuming its order, and combine compatible SELECT results with UNION.
How it connects
Connects result sets to SELECT, UNION, ORDER BY, column aliases, data types, and set operators.

Also explained in

Compare how 4 books explain database result set →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.