SQL Intro
SQL is the language used to ask questions of a database and to change the data it holds. A database stores its data in tables, and each table holds rows and columns.
A first query
This query asks for the name of the customer with id 1. The answer is one row with one column.
SELECT name FROM customers WHERE customer_id = 1;One column of a table
This query asks for the name of every supplier. The suppliers table holds three rows, so the answer holds three rows.
SELECT name FROM suppliers;A question with a filter
The WHERE part narrows a question to the rows that match. This query asks for the name of the customer who lives in Japan.
SELECT name FROM customers WHERE country = 'Japan';Example
SELECT city FROM customers WHERE name = 'Ana Silva';Key points
- Database: a stored collection of data organised in tables
- Table: a set of rows and columns, like a price list
- Row: one record in a table, like one customer
- Column: one kind of value in a table, like a name
- Query: a question written in SQL that returns rows
- The sample shop database holds four tables named customers, products, orders and suppliers.
- SQLite: the small database engine that runs the queries in this course
In this example, the query asks the customers table for one column, the city. The WHERE part picks the row whose name is Ana Silva. The answer is the single value Lisbon.
Try it Yourself
Edit the code, then press Run.
Running is simulated Β· DB Fiddle
The customers table
| customer_id | name | city | country |
|---|---|---|---|
| 1 | Ana Silva | Lisbon | Portugal |
| 2 | Ben Carter | Austin | USA |
| 3 | Chen Wei | Toronto | Canada |
| 4 | Dana Novak | Prague | Czechia |
| 5 | Eli Ross | Austin | USA |
| 6 | Fatima Noor | Dubai | UAE |
| 7 | Gus Meyer | Berlin | Germany |
| 8 | Hana Sato | NULL | Japan |
Exercise
What does a query return?