What SQL is
SQL, short for Structured Query Language, is the standard language for asking a relational database questions and for changing the data in it. It reads a little like English, which makes the basics approachable. Different database products add their own extras, but the core statements in this article work in most of them.
A query is a request for data. Most first queries use a single statement, SELECT, which reads data without changing it.
A small practice table
The examples use a hypothetical table called employees:
| id | name | department | salary | start_year |
|---|---|---|---|---|
| 1 | Amira | Support | 38000 | 2021 |
| 2 | Jonas | Networks | 45000 | 2019 |
| 3 | Priya | Support | 41000 | 2018 |
| 4 | Marcus | Security | 52000 | 2020 |
| 5 | Lena | Networks | 47000 | 2022 |
The figures are invented for illustration only.
Selecting everything
The simplest query reads every column and every row:
SELECT * FROM employees;
SELECT says what you want, the asterisk means “all columns”, and FROM names the table. The semicolon ends the statement. Keywords are not case sensitive, but writing them in capitals helps readability.
Using * is fine for exploring a table. In real work, naming the columns you need is clearer and often faster.
Selecting particular columns
SELECT name, department FROM employees;
This returns only two columns, for all five rows. The order you list the columns is the order they appear.
Filtering rows with WHERE
WHERE limits which rows come back:
SELECT name, salary
FROM employees
WHERE department = 'Support';
This returns Amira and Priya. Text values go inside single quotes. Numbers do not need quotes.
Comparison operators include =, <> (not equal), <, >, <= and >=. For example, WHERE salary > 45000 returns Marcus (52000) and Lena (47000). Jonas earns exactly 45000, so he is excluded because the comparison is strictly greater than.
Combining conditions
Use AND, OR and parentheses:
SELECT name
FROM employees
WHERE department = 'Networks' AND start_year >= 2020;
Only Lena matches, since Jonas started in 2019. With AND, both conditions must be true. With OR, either one is enough. When mixing the two, add parentheses so the meaning is unmistakable.
Matching patterns and lists
LIKE matches text patterns, where % stands for any run of characters:
SELECT name FROM employees WHERE name LIKE 'P%';
This finds names starting with P, here Priya. IN checks against a list:
SELECT name FROM employees WHERE department IN ('Support', 'Security');
That returns Amira, Priya and Marcus.
Sorting with ORDER BY
Without ORDER BY, the database does not promise any particular row order. To sort:
SELECT name, salary
FROM employees
ORDER BY salary DESC;
DESC means descending, highest first, giving Marcus, Lena, Jonas, Priya, Amira. ASC, the default, sorts ascending. You can sort by more than one column, separated by commas, such as ORDER BY department, salary DESC.
Limiting results
To see just the top few rows, add a limit. Many systems use LIMIT:
SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 2;
That returns Marcus and Lena. Some database products use different wording for the same idea, such as TOP or FETCH FIRST, so check the documentation for yours.
Counting and summarizing
SQL can also summarize. COUNT(*) counts rows:
SELECT COUNT(*) FROM employees WHERE department = 'Support';
The answer is 2. Other summary functions include SUM, AVG, MIN and MAX. For example, the average of the five invented salaries is (38000 + 45000 + 41000 + 52000 + 47000) / 5 = 223000 / 5 = 44600. Grouping results by department with GROUP BY is a natural next step once these basics are comfortable.
Missing values and NULL
A missing value in a database is stored as NULL, which means “unknown”, not zero or an empty string. Because of this, WHERE salary = NULL never works as you would hope. Use:
SELECT name FROM employees WHERE salary IS NULL;
Or IS NOT NULL for the opposite. Unexpected NULLs are a common source of puzzling results, and they are one of the issues covered in data cleaning.
Safe habits for beginners
- Practice on a copy. Use a sample or test database, never the live one. A home lab is a good place for this, and a small local database is enough.
- Read before you write.
SELECTis harmless. Statements that change data, such asUPDATEandDELETE, deserve more care, and a missingWHEREclause can affect every row. Test with aSELECTusing the sameWHEREfirst. - Keep a recovery route. Before changing anything important, make sure a recent copy exists. A sound backup strategy is what makes mistakes recoverable.
- Use limited accounts. Day-to-day querying rarely needs powerful permissions, which fits the principle of least privilege.
Running queries
Queries are run through a database client, which might be a graphical tool, a web console or a text-based program. Command line basics help if you use the text-based kind. Programs can also send queries on your behalf: Python basics will take you as far as a script that connects to a database and prints results.
The order SQL is written versus how it thinks
You write SELECT first, but the database logically works out FROM first, then WHERE, then the rest, finally sorting and limiting. That is why you cannot always use a column alias from the SELECT list inside WHERE. It is a small detail now, but it explains several early error messages.
When you are comfortable with these statements, the usual next step is combining tables, which is the subject of SQL joins.