What Is SQL?
SQL (Structured Query Language) is a feature-rich language for querying databases. SQL queries are more properly called statements.
This note covers the simplest commands: retrieving (select), updating, inserting and deleting data. Some database servers have their own syntax and small differences in how things work. All of these examples are based on a MySQL database; once the basics are down, the alternative syntax for other servers is easy to look up online. SQL syntax is not case sensitive.
SELECT
The first query type is SELECT, which retrieves data from the database.
select * from users;
| id | username | password |
|---|---|---|
| 1 | jon | pass123 |
| 2 | admin | p4ssword |
| 3 | martin | secret123 |
SELECTtells the database we want to retrieve some data.*asks for all columns in the table back. Here the table has three columns (id, username and password).- "from users" says to retrieve the data from the table named users.
- The semicolon at the end marks the end of the query.
Selecting Specific Columns
This query is like the one above, but instead of * returning every column, it only asks for the username and password fields.
select username,password from users;
| username | password |
|---|---|
| jon | pass123 |
| admin | p4ssword |
| martin | secret123 |
LIMIT
This query returns all columns with the * selector, like the first one, and the "LIMIT 1" clause forces the database to return only one row of data.
select * from users LIMIT 1;
| id | username | password |
|---|---|---|
| 1 | jon | pass123 |
Changing it to "LIMIT 1,1" makes the query skip the first result, "LIMIT 2,1" skips the first two results, and so on. The first number is how many results to skip; the second number is how many rows to return.
WHERE
The where clause picks out exactly the data needed by returning only rows that match specific clauses:
select * from users where username='admin';
| id | username | password |
|---|---|---|
| 2 | admin | p4ssword |
This only returns the rows where the username is equal to admin.
select * from users where username != 'admin';
| id | username | password |
|---|---|---|
| 1 | jon | pass123 |
| 3 | martin | secret123 |
This only returns the rows where the username is NOT equal to admin.
select * from users where username='admin' or username='jon';
| id | username | password |
|---|---|---|
| 1 | jon | pass123 |
| 2 | admin | p4ssword |
This only returns the rows where the username is equal to either admin or jon.
select * from users where username='admin' and password='p4ssword';
| id | username | password |
|---|---|---|
| 2 | admin | p4ssword |
This only returns the rows where the username is equal to admin and the password is equal to p4ssword.
LIKE
The like clause matches data that is not an exact match but starts with, contains or ends with certain characters. Where the wildcard character (a percentage sign, %) is placed decides which.
select * from users where username like 'a%';
| id | username | password |
|---|---|---|
| 2 | admin | p4ssword |
This returns any rows with a username beginning with the letter a.
select * from users where username like '%n';
| id | username | password |
|---|---|---|
| 1 | jon | pass123 |
| 2 | admin | p4ssword |
| 3 | martin | secret123 |
This returns any rows with a username ending with the letter n.
select * from users where username like '%mi%';
| id | username | password |
|---|---|---|
| 2 | admin | p4ssword |
This returns any rows with a username containing the characters mi.
UNION
The UNION statement combines the results of two or more SELECT statements to retrieve data from one or more tables. The rules:
- Each SELECT statement must retrieve the same number of columns.
- The columns have to be of a similar data type.
- The column order has to be the same.
That might not sound very clear, so here is an analogy. A company wants a list of addresses for all customers and suppliers to post out a new catalogue. One table, called customers, has these contents:
| id | name | address | city | postcode |
|---|---|---|---|---|
| 1 | Mr John Smith | 123 Fake Street | Manchester | M2 3FJ |
| 2 | Mrs Jenny Palmer | 99 Green Road | Birmingham | B2 4KL |
| 3 | Miss Sarah Lewis | 15 Fore Street | London | NW12 3GH |
And another, called suppliers, has these contents:
| id | company | address | city | postcode |
|---|---|---|---|---|
| 1 | Widgets Ltd | Unit 1a, Newby Estate | Bristol | BS19 4RT |
| 2 | The Tool Company | 75 Industrial Road | Norwich | N22 3DR |
| 3 | Axe Makers Ltd | 2b Makers Unit, Market Road | London | SE9 1KK |
This SQL statement gathers the results from both tables into one result set:
SELECT name,address,city,postcode from customers UNION SELECT company,address,city,postcode from suppliers;
| name | address | city | postcode |
|---|---|---|---|
| Mr John Smith | 123 Fake Street | Manchester | M2 3FJ |
| Mrs Jenny Palmer | 99 Green Road | Birmingham | B2 4KL |
| Miss Sarah Lewis | 15 Fore Street | London | NW12 3GH |
| Widgets Ltd | Unit 1a, Newby Estate | Bristol | BS19 4RT |
| The Tool Company | 75 Industrial Road | Norwich | N22 3DR |
| Axe Makers Ltd | 2b Makers Unit, Market Road | London | SE9 1KK |
INSERT
The INSERT statement tells the database to insert a new row of data into a table.
- "into users" says which table to insert the data into.
- "(username,password)" lists the columns we are providing data for.
- "values ('bob','password');" provides the data for those columns.
insert into users (username,password) values ('bob','password123');
| id | username | password |
|---|---|---|
| 1 | jon | pass123 |
| 2 | admin | p4ssword |
| 3 | martin | secret123 |
| 4 | bob | password123 |
UPDATE
The UPDATE statement tells the database to update one or more rows of data in a table.
- "update %tablename% SET" names the table to update.
- Then the field or fields to update follow as a comma-separated list, such as "username='root',password='pass123'".
- Finally, like with SELECT, the where clause picks exactly which rows to update, such as "where username='admin;".
update users SET username='root',password='pass123' where username='admin';
| id | username | password |
|---|---|---|
| 1 | jon | pass123 |
| 2 | root | pass123 |
| 3 | martin | secret123 |
| 4 | bob | password123 |
DELETE
The DELETE statement tells the database to delete one or more rows of data. Apart from not listing the columns to return, the format is very similar to SELECT. The where clause picks exactly which data to delete, and the LIMIT clause sets how many rows get deleted.
delete from users where username='martin';
| id | username | password |
|---|---|---|
| 1 | jon | pass123 |
| 2 | root | pass123 |
| 4 | bob | password123 |
delete from users;
Because no WHERE clause was used in this query, all the data in the table is deleted.
| id | username | password |
|---|