liveBook · Manning

SQL

As explained in Learn AI Data Engineering

A search query is a set of terms or structured input submitted to a search system to retrieve relevant results.

A SQL query expresses a data question as executable SQL. In an AI-assisted workflow, a plain-language request can produce a starting query, but the generated logic must be run against the actual database and checked against the intended meaning. section 1.5 section 3.1

A request such as “most popular film” needs operational detail. Counting rentals may be a useful interpretation, yet store, customer activity, date range, lost rentals, and refunded rentals can change what the question should mean. Refine the prompt when those conditions matter. section 2.3.2

Find the most rented film

This query joins films, inventory, and rentals; groups by title; counts rental records; sorts descending; and returns one result. Run it in pgAdmin and verify that its definition of popularity matches the requirement. section 2.3.2

SELECT f.title, COUNT(r.rental_id) AS total_rentals
FROM film f
JOIN inventory i ON f.film_id = i.film_id
JOIN rental r ON i.inventory_id = r.inventory_id
GROUP BY f.title
ORDER BY total_rentals DESC
LIMIT 1;

Inspect a complete query that uses joins, aggregation, ordering, and a limit to answer a rental-popularity question. section 2.3.2

When a query can be expressed in several forms, ask for two solutions and compare them. Joins, subqueries, CTEs, nested selects, and aggregation strategies can differ while aiming at the same result. Divergent outputs are a signal to review the logic closely; matching results should still be confirmed by execution. section 3.4

A practical query-generation loop is:

  • State the dataset and desired output clearly, including filters or constraints that define the metric. section 2.3.2
  • Extract the SQL from the model response and execute it in pgAdmin. section 3.1
  • Check results against expectations; revise the prompt if the query misses the requirement. section 2.4
  • For uncertain logic, generate alternative queries and compare their results. section 3.4

How Learn AI Data Engineering covers it

How deep it goes
Works through prompting, executing, inspecting, and comparing queries rather than SQL language internals.
Examples
Uses Pagila film rentals, inventory counts, title_count views, and customer-rental thresholds as query tasks.
What it assumes
Assumes access to ChatGPT, pgAdmin, and a Pagila database while teaching prompt-and-validate query practice.
What you can do after
Apply it via ChatGPT and pgAdmin to generate, run, verify, refine, and compare SQL queries.
How it connects
Connects queries to zero-shot prompting, self-consistency prompting, joins, subqueries, CTEs, views, and aggregation.

Also explained in

Compare how 31 books explain SQL →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI Data Engineering. The section links point to the parts of the book it draws on.