Beginner Logical Thinking SQL

Basic SQL Queries with Examples and Output

Write the 15 basic SELECT queries every beginner needs and predict their result tables.

  • TimeEach query reads all rows of this small table, so O(n); ORDER BY adds sorting, O(n log n)
  • SpaceO(n) for the result set in the worst case
  • Hints3
  • Read5 min
Quick answer

The basic SQL queries are SELECT to choose columns, WHERE to filter rows, AND and OR to join conditions, ORDER BY to sort, LIMIT to take the first few rows, DISTINCT to remove repeats, COUNT, SUM, AVG, MAX and MIN to summarise, GROUP BY to summarise per group, LIKE for patterns and BETWEEN for ranges. Practise them on one small table until you can predict each result before running it.

SELECT and WHEREORDER BY and LIMITAggregate functionsGROUP BY
1

Problem statement

Create the employees table below and run 15 basic SQL queries on it. Before running each one, guess the result. Then check your guess with the result table.

emp_id name department salary city
1 Ravi IT 60000 Hyderabad
2 Priya HR 45000 Chennai
3 Kiran IT 75000 Bengaluru
4 Sneha Finance 55000 Hyderabad
5 Arjun IT 51000 Pune
6 Divya HR 52000 Hyderabad
7 Rahul Finance 68000 Chennai
8 Anjali Sales 40000 Pune

When you finish, try the SQL practice questions with answers, which use this same table.

Input

The employees table above, created with the script in the solution.

Output

The result table of each query.

Examples

Example 1
InputSELECT name FROM employees WHERE department = 'IT';
OutputRavi, Kiran, Arjun

Only the three rows with department IT pass the WHERE filter.

Example 2
InputSELECT COUNT(*) FROM employees;
Output8

COUNT(*) counts every row.

Constraints

  • Written in standard SQL that runs on MySQL 8; the outputs were checked by running the script
  • 8 rows, 5 columns
2

Think before you code

Reveal the questions to ask yourself first
  • Which part of a query picks the columns, and which part picks the rows?
  • Which runs first, WHERE or ORDER BY?
  • What does a GROUP BY query return: one row per employee or one row per group?
3

Hints

Open one at a time — try again after each.
1 Hint 1
Text values go in single quotes: WHERE city = 'Pune'. Numbers do not.
2 Hint 2
Without ORDER BY, SQL does not promise any row order. Add ORDER BY when order matters.
3 Hint 3
In a GROUP BY query, every selected column must be in GROUP BY or inside an aggregate like COUNT or AVG.
4

Approach

Reveal the step-by-step approach

Order of clauses

Write the clauses in this order. Skip the ones you do not need.

SELECT columns
FROM table
WHERE row_condition
GROUP BY column
HAVING group_condition
ORDER BY column
LIMIT n;

Which clause does what

Clause Job Query numbers below
SELECT choose columns all
WHERE filter rows 3, 4, 5, 6, 14, 15
ORDER BY sort 7, 8
LIMIT first n rows 8
DISTINCT remove repeated values 9
COUNT, SUM, AVG, MAX, MIN summarise 10, 11
GROUP BY summarise per group 12, 13
LIKE, BETWEEN pattern and range filters 14, 15

New to SQL? Start with what is SQL and the SELECT statement.

5

Dry run

Walk through the example step by step

Query 5: WHERE city = 'Hyderabad' AND salary > 50000. Each row is tested:

name   | city      | salary | city ok? | salary ok? | kept
-------+-----------+--------+----------+------------+-----
Ravi   | Hyderabad | 60000  | yes      | yes        | yes
Priya  | Chennai   | 45000  | no       | no         | no
Kiran  | Bengaluru | 75000  | no       | yes        | no
Sneha  | Hyderabad | 55000  | yes      | yes        | yes
Arjun  | Pune      | 51000  | no       | yes        | no
Divya  | Hyderabad | 52000  | yes      | yes        | yes
Rahul  | Chennai   | 68000  | no       | yes        | no
Anjali | Pune      | 40000  | no       | no         | no

With AND, both checks must be yes. Three rows are kept.

6

Solution

Reveal the full SQL solution
solution.sql
CREATE TABLE employees (
    emp_id     INT PRIMARY KEY,
    name       VARCHAR(50),
    department VARCHAR(30),
    salary     INT,
    city       VARCHAR(30)
);

INSERT INTO employees (emp_id, name, department, salary, city) VALUES
(1, 'Ravi',   'IT',      60000, 'Hyderabad'),
(2, 'Priya',  'HR',      45000, 'Chennai'),
(3, 'Kiran',  'IT',      75000, 'Bengaluru'),
(4, 'Sneha',  'Finance', 55000, 'Hyderabad'),
(5, 'Arjun',  'IT',      51000, 'Pune'),
(6, 'Divya',  'HR',      52000, 'Hyderabad'),
(7, 'Rahul',  'Finance', 68000, 'Chennai'),
(8, 'Anjali', 'Sales',   40000, 'Pune');

-- 1. All rows and columns
SELECT * FROM employees;
-- 2. Only some columns
SELECT name, salary FROM employees;
-- 3. Filter with WHERE
SELECT * FROM employees WHERE department = 'IT';
-- 4. Number comparison
SELECT name, salary FROM employees WHERE salary > 55000;
-- 5. Two conditions with AND
SELECT name, city, salary FROM employees WHERE city = 'Hyderabad' AND salary > 50000;
-- 6. Either condition with OR
SELECT name, department FROM employees WHERE department = 'HR' OR department = 'Sales';
-- 7. Sort high to low
SELECT name, salary FROM employees ORDER BY salary DESC;
-- 8. Top 3 salaries
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 3;
-- 9. Unique cities
SELECT DISTINCT city FROM employees ORDER BY city;
-- 10. Count rows
SELECT COUNT(*) AS total_employees FROM employees;
-- 11. Sum, average, highest, lowest
SELECT SUM(salary) AS total, AVG(salary) AS average,
       MAX(salary) AS highest, MIN(salary) AS lowest
FROM employees;
-- 12. Count per department
SELECT department, COUNT(*) AS employees
FROM employees GROUP BY department ORDER BY department;
-- 13. Average salary per department
SELECT department, AVG(salary) AS avg_salary
FROM employees GROUP BY department ORDER BY department;
-- 14. Names starting with A
SELECT name FROM employees WHERE name LIKE 'A%';
-- 15. Salary in a range
SELECT name, salary FROM employees WHERE salary BETWEEN 50000 AND 60000 ORDER BY salary;

Results of each query

Query 1 returns the whole table shown above, all 8 rows.

Query 2: SELECT name, salary

name salary
Ravi 60000
Priya 45000
Kiran 75000
Sneha 55000
Arjun 51000
Divya 52000
Rahul 68000
Anjali 40000

Query 3: WHERE department = 'IT'

emp_id name department salary city
1 Ravi IT 60000 Hyderabad
3 Kiran IT 75000 Bengaluru
5 Arjun IT 51000 Pune

Query 4: WHERE salary > 55000. Sneha has exactly 55000, so she is not included.

name salary
Ravi 60000
Kiran 75000
Rahul 68000

Query 5: city = 'Hyderabad' AND salary > 50000

name city salary
Ravi Hyderabad 60000
Sneha Hyderabad 55000
Divya Hyderabad 52000

Query 6: department = 'HR' OR department = 'Sales'. WHERE department IN ('HR', 'Sales') gives the same rows.

name department
Priya HR
Divya HR
Anjali Sales

Query 7: ORDER BY salary DESC

name salary
Kiran 75000
Rahul 68000
Ravi 60000
Sneha 55000
Divya 52000
Arjun 51000
Priya 45000
Anjali 40000

Query 8: ORDER BY salary DESC LIMIT 3

name salary
Kiran 75000
Rahul 68000
Ravi 60000

Query 9: SELECT DISTINCT city

city
Bengaluru
Chennai
Hyderabad
Pune

Query 10: COUNT(*)

total_employees
8

Query 11: aggregates

total average highest lowest
446000 55750.0000 75000 40000

MySQL shows AVG of an INT column with four decimal places. SQLite shows the same value as 55750.0.

Query 12: GROUP BY department with COUNT(*)

department employees
Finance 2
HR 2
IT 3
Sales 1

Query 13: GROUP BY department with AVG(salary)

department avg_salary
Finance 61500.0000
HR 48500.0000
IT 62000.0000
Sales 40000.0000

Query 14: LIKE 'A%'. % means any number of characters after A.

name
Arjun
Anjali

Query 15: BETWEEN 50000 AND 60000. Both ends are included, so Ravi's 60000 counts.

name salary
Arjun 51000
Divya 52000
Sneha 55000
Ravi 60000

Queries 1 to 6 and 14 have no ORDER BY. MySQL usually returns them in primary key order as shown, but SQL does not promise this.

Next steps

TimeEach query reads all rows of this small table, so O(n); ORDER BY adds sorting, O(n log n)
SpaceO(n) for the result set in the worst case
7

Review before the interview

Common mistakes

  • Using double quotes for text values. MySQL often accepts them, but single quotes are standard SQL.
  • Writing WHERE after ORDER BY. The clause order is fixed: WHERE comes before GROUP BY and ORDER BY.
  • Selecting name together with COUNT(*) GROUP BY department. name is not grouped, so MySQL 8 rejects it by default.
  • Writing WHERE COUNT(*) > 1. Filters on aggregates go in HAVING, not WHERE.
  • Thinking BETWEEN 50000 AND 60000 excludes the end values. It includes both.

Edge cases to test

  • A WHERE condition that matches nothing returns an empty result, not an error.
  • COUNT(*) counts all rows; COUNT(column) skips rows where that column is NULL.
  • Text comparison in MySQL is usually not case-sensitive with the default collation, so 'pune' also matches 'Pune'.
  • LIMIT without ORDER BY gives "some" rows, not the "top" rows.

Interview follow-ups

  • How would you find employees whose salary is above the company average?
  • What is the difference between WHERE and HAVING?
  • How would you show the department with the highest total salary?

Keep practising

Practising SQL for interviews?

CodeBegun’s Data Analytics with AI program builds SQL skills on real business data, with mentor review and mock interviews.

Chat with us