toolrota

What is SQL: the database query language and basic queries with SELECT, WHERE and JOIN

SQL is a valuable skill for anyone who works with data. We explain basic queries and concepts with simple examples.

Serkan Akyürek··4 min read
What is SQL: the database query language and basic queries with SELECT, WHERE and JOIN – SaaS & Cloud

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

ConceptMeaning
TableA dataset of rows and columns
Row (record)A single record, for example a customer
Column (field)A property, for example city
Primary keyThe column that uniquely identifies each record
Foreign keyA 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).

How to think of a SQL query
  1. 1

    Decide which table to take data from (FROM)

  2. 2

    Filter which rows you want (WHERE)

  3. 3

    Group if needed (GROUP BY)

  4. 4

    Choose which columns to show (SELECT)

  5. 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 typeResult
INNER JOINRecords matching in both tables
LEFT JOINAll of the left table, matches from the right
RIGHT JOINAll of the right table
FULL JOINAll 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

  1. Table, row, column concepts.
  2. SELECT, WHERE, ORDER BY.
  3. GROUP BY and aggregate functions.
  4. JOIN.
  5. INSERT/UPDATE/DELETE and security.
  6. Indexes and performance.

Common mistakes

  1. Running UPDATE or DELETE without WHERE.
  2. Using SELECT * needlessly.
  3. Inserting user input directly into a query.
  4. Setting up the JOIN condition wrongly and duplicating records.
  5. 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.

Share
Published:
Author
Serkan Akyürek

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.