
When you want to say "get the customers in London" from a customer table, the language for talking to the database is SQL (Structured Query Language). SQL is the standard language used to query, add, update and delete data in relational databases. This guide covers core concepts and queries with examples. For the database concept see our database guide.
Core concepts
| Concept | Meaning |
|---|---|
| Table | A dataset of rows and columns |
| Row (record) | A single record, for example a customer |
| Column (field) | A property, for example city |
| Primary key | The column that uniquely identifies each record |
| Foreign key | A column linking to a record in another table |
SELECT: getting data
Basic form: you state which columns to fetch from a table. An asterisk is used for all columns, but selecting needed columns is more efficient.
SELECT name, city FROM customers;
WHERE: filtering
SELECT name FROM customers WHERE city = 'London';
Conditions use AND, OR, NOT, comparison (>, <, =), LIKE and IN. It resembles filtering logic in Excel (see our sort and filter guide).
ORDER BY and LIMIT
SELECT name, amount FROM orders ORDER BY amount DESC LIMIT 10;
It fetches the ten highest amounts; DESC sorts from high to low.
GROUP BY: grouping and aggregating
SELECT city, COUNT(*) FROM customers GROUP BY city;
COUNT, SUM, AVG, MIN and MAX are aggregate functions; similar to pivot tables and SUMIFS in Excel (see our pivot table and SUMIFS guides).
- 1
Decide which table to take data from (FROM)
- 2
Filter which rows you want (WHERE)
- 3
Group if needed (GROUP BY)
- 4
Choose which columns to show (SELECT)
- 5
Sort and limit (ORDER BY, LIMIT)
JOIN: combining tables
In a relational database data is kept in several tables; JOIN combines them on a common key.
| JOIN type | Result |
|---|---|
| INNER JOIN | Records matching in both tables |
| LEFT JOIN | All of the left table, matches from the right |
| RIGHT JOIN | All of the right table |
| FULL JOIN | All of both tables |
Example: combining customers and orders on the customer number. It is similar to VLOOKUP logic in Excel (see our VLOOKUP and XLOOKUP guides).
Inserting, updating and deleting data
- INSERT: adds a new record.
- UPDATE: changes an existing record; the WHERE condition is very important.
- DELETE: deletes records; again with WHERE.
Note: if you forget WHERE in UPDATE or DELETE the whole table is affected. First look at which rows will be affected with SELECT and take a backup (see our backup guide).
Security: SQL injection
Inserting text from a user directly into a query can let an attacker change the query. The fix: use parameterised queries, validate input and give the database user least privilege. Don't write passwords and keys in code (see our API key security guide).
Which SQL?
Systems such as MySQL, PostgreSQL, SQL Server and SQLite share basic SQL; there are small syntax differences. A common choice for websites is MySQL (see our WordPress guide).
Learning roadmap
- Table, row, column concepts.
- SELECT, WHERE, ORDER BY.
- GROUP BY and aggregate functions.
- JOIN.
- INSERT/UPDATE/DELETE and security.
- Indexes and performance.
Common mistakes
- Running UPDATE or DELETE without WHERE.
- Using SELECT * needlessly.
- Inserting user input directly into a query.
- Setting up the JOIN condition wrongly and duplicating records.
- Experimenting on live data without a backup.
Frequently asked questions
Is SQL a programming language? It is more a query/declarative language.
Does Excel knowledge help with SQL? Yes; filter, pivot and lookup logic are similar.
What is NoSQL? Databases that use non-table data models.
Summary
SQL is the standard language for working with relational databases. SELECT, WHERE, GROUP BY and JOIN are the cornerstones; be careful with modifying commands and use parameterised queries.
Serkan Akyürek is the founder and author of toolrota; he writes step-by-step guides on Excel, AI, the cloud, personal finance and digital security.