Sitemap

Top SQL Commands Every IT Professional Must Know (Interview Guide)

7 min readMar 10, 2026

--

Press enter or click to view image in full size
Photo by Caspar Camille Rubin on Unsplash

SQL is one of the most important skills for IT careers, especially for roles like:

  • Data Analyst
  • Data Scientist
  • Backend Developer
  • Database Administrator
  • Data Engineer

Almost all technical interviews for these roles include SQL questions. If you understand the core SQL commands, you will be able to retrieve, manipulate, and analyze data in an efficient manner. The guide discusses frequently asked SQL commands and concepts, along with simple examples.

What is SQL?

SQL (Structured Query Language) is a programming language mainly developed for managing and editing data stored in a relational database.

It allows users to:

  • Retrieve data
  • Insert new records
  • Update existing data
  • Delete records
  • Create and manage tables

Example Table Used in This Article:

To understand the queries, assume we are working with the following table:

Sales (
OrderID INT,
year INT,
month VARCHAR(20),
Country VARCHAR(50),
City VARCHAR(50),
west DECIMAL,
Sale_amount DECIMAL,
CustomerID INT,
ProductID INT
)

Core SQL Commands:

  1. SELECT — Retrieving records from Sales table
SELECT * FROM Sales;

* means select all columns.

You can also select specific columns:

SELECT year, month, west FROM Sales;

Aliasing Columns:

SELECT west AS "West Region" FROM Sales;

Aliases make output more readable.

2. WHERE — Retrieving records that meet specific conditions

SELECT * FROM Sales
WHERE Country = 'Canada';

Comparison Operators:

= Equal

!= or <> Not equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

SELECT * FROM Sales
WHERE Sale_amount <= 50000;

3. ORDER BY — Sort query results

SELECT * FROM Sales
ORDER BY Sale_amount DESC;

*DESC → highest to lowest

*ASC → lowest to highest

4. LIMIT — Restricting the number of rows returned in the output

SELECT * FROM Sales
LIMIT 100;

Useful when working with large datasets.

Logical Operators:

SQL allows combining multiple conditions:

AND, OR, NOT, LIKE, IN, BETWEEN, IS NULL

SELECT * FROM Sales
WHERE Country = 'Japan'
AND (City = 'Goa' OR City = 'Paris');

5. LIKE — Search for a specified pattern

SELECT * FROM Sales
WHERE group LIKE 'New%';

6. IN — Match a column against multiple possible values

SELECT * FROM Sales
WHERE Country IN('India', 'Nepal', 'UK');

Aggregate Functions: COUNT(), SUM(), MIN(), MAX(), AVG()

SELECT COUNT(*) FROM Sales;
SELECT SUM(Sale_amount) FROM Sales;

7. GROUP BY — Used with aggregate functions to group data

SELECT year, COUNT(*) AS count
FROM Sales
GROUP BY year;

Multiple columns:

SELECT year, month, COUNT(*)
FROM Sales
GROUP BY year, month;

8. HAVING — Filtering Aggregated Results

You cannot use WHERE with aggregate functions. Instead, use HAVING.

SELECT year, month, MAX(high) AS month_high
FROM Sales
GROUP BY year, month
HAVING MAX(high) > 400;

9. JOINs —Combining Data From Multiple Tables (Interview Favorite!)

(i) INNER JOIN — Returns matching records in both tables

SELECT *
FROM table1
INNER JOIN table2
ON table1.id = table2.id;

(ii) LEFT JOIN — Returns all records from left table + matching from right

SELECT *
FROM table1
LEFT JOIN table2
ON table1.id = table2.id;

(iii) RIGHT JOIN — Returns all records from right table + matching from left

SELECT *
FROM table1
RIGHT JOIN table2
ON table1.id = table2.id;

(iv) FULL JOIN (FULL OUTER JOIN)— All rows from both tables

SELECT *
FROM table1
FULL OUTER JOIN table2
ON table1.id = table2.id;

(v) CROSS JOIN — Returns the Cartesian product

SELECT *
FROM table1
CROSS JOIN table2;

10. UNION — Unlike JOIN (side-by-side), UNION stacks datasets vertically

SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;

Use UNION ALL to include duplicates.

Get Bidyut Das’s stories in your inbox

Join Medium for free to get updates from this writer.

11. Subqueries — A query inside another query

SELECT *
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);

(i) EXISTS — Returns TRUE if the subquery returns at least one record

SELECT column_name
FROM table_name
WHERE EXISTS (
SELECT column_name
FROM another_table
WHERE condition
);

(ii) ANY and ALL — Used to compare a value against a subquery

SELECT ProductName
FROM Sales
WHERE ProductID = ANY (
SELECT ProductID
FROM OrderDetails
WHERE Quantity > 99
);

ANY → True if the condition matches at least one value.

ALL → True only if the condition matches all values.

12. DISTINCT — Removing Duplicates (Unique values)

SELECT DISTINCT month FROM Sales;

13. CASE Statement (Conditional Logic) — SQL’s version of IF-THEN logic

SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN 'High'
WHEN Quantity = 30 THEN 'Medium'
ELSE 'Low'
END AS Quantity_Text
FROM Sales;

Must always end with END.

14. CREATE TABLE — employees Table Creation

CREATE TABLE employees (
PersonID INT PRIMARY KEY,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
);

Constraints: PRIMARY KEY, FOREIGN KEY,

CREATE TABLE employees (
id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE,
age INT CHECK (age >= 18),
department_id INT,
CONSTRAINT fk_department
FOREIGN KEY (department_id)
REFERENCES departments(id)
);

15. ALTER TABLE — Modify the structure of an existing table

ALTER TABLE employees
ADD email VARCHAR(150);

16. INSERT INTO — Add new records (rows) into a table

INSERT INTO table_name (column1, column2)
VALUES (value1, value2);

Or:

INSERT INTO table_name
VALUES (value1, value2, value3);

17. UPDATE — Update records in Sales table

UPDATE Sales
SET city = 'Goa'
WHERE CustomerID = 1;

If you omit the WHERE clause, ALL records will be updated.

18. DELETE — Delete records

DELETE FROM Sales
WHERE CustomerName = 'Bob';

Delete all records:

DELETE FROM Sales;

NULL: A NULL value means no value exists.

You cannot check NULL using =.

Use:

WHERE Address IS NULL;

Or:

WHERE Address IS NOT NULL;

19. DROP — Deletes the Sales table and all its data.

DROP TABLE Sales;

SQL Command Categories:

(i) DDL — Data Definition Language (Defines database structure)

CREATE, ALTER, DROP, TRUNCATE, RENAME

(ii) DML — Data Manipulation Language (Modifies table data)

INSERT, UPDATE, DELETE

(iii) DQL — Data Query Language (Retrieves data)

SELECT

(iv) DCL — Data Control Language (Controls permissions)

GRANT, REVOKE

(v) TCL — Transaction Control Language (Manages transactions)

COMMIT, ROLLBACK, SAVEPOINT

Top SQL Interview Questions:

  1. What is the difference between WHERE and HAVING?

Answer: (i) WHERE filters rows before grouping (ii) HAVING filters data after GROUP BY

2. What happens if you use NULL = NULL?

Answer: It returns UNKNOWN, not TRUE. Correct way: WHERE column IS NULL;

3. What is the difference between DELETE, TRUNCATE, and DROP?

Answer: (i) DELETE removes selected rows using a WHERE clause and can usually be rolled back. (ii) TRUNCATE removes all rows at once, is faster than DELETE, and typically cannot be rolled back. (iii) DROP completely deletes the table itself, including its structure and data.

4. Difference Between INNER JOIN, LEFT JOIN, and RIGHT JOIN?

Answer: (i) INNER JOIN returns only the rows that have matching values in both tables. (ii) LEFT JOIN returns all rows from the left table and the matching rows from the right table (non-matching right rows show as NULL). (iii) RIGHT JOIN returns all rows from the right table and the matching rows from the left table (non-matching left rows show as NULL).

5. Find the 2nd Highest Salary?

Answer: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);

6. What is the difference between UNION and UNION ALL?

Answer: (i)UNION combines results from two queries and removes duplicate rows, which makes it slower because it performs an additional distinct operation. (ii)UNION ALL also combines results from two queries but keeps duplicates, making it faster and more efficient when duplicate removal is not needed.

7. Difference Between PRIMARY KEY and UNIQUE?

Answer: (i) A PRIMARY KEY uniquely identifies each row in a table, does not allow NULL values, and only one primary key can exist per table. (ii) A UNIQUE constraint also ensures uniqueness, but it can allow one NULL value (in most databases), and multiple UNIQUE constraints can exist in a table.

8. What is Index? Why is it used?

Answer: An index is a database object that improves the speed of data retrieval operations on a table by creating a quick lookup structure (similar to a book index). It is used to improve query performance, especially for large tables, but it can slightly slow down INSERT, UPDATE, and DELETE operations because the index must also be updated.

9. What is the difference between COUNT(*) and COUNT(column_name)?

Answer: (i)COUNT(*) counts all rows (including NULLs). (ii)COUNT(column_name) counts only non-NULL values in that column.

10. What is the difference between GROUP BY and DISTINCT?

Answer: (i)DISTINCT removes duplicate rows. (ii)GROUP BY group rows to perform aggregate functions like COUNT(), SUM(), AVG().

11. Find Duplicate Records in a Table

Answer: SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;

12. What is SQL Injection?

Answer: A security vulnerability where malicious SQL is inserted into queries.

References & Further Reading:

  1. SQL Cheat Sheet for interview 2026 (2025) AlmaBetter. Available at: https://www.almabetter.com/bytes/cheat-sheet/sql (Accessed: 23 February 2026).
  2. Custer, C. (2024). SQL commands: The Complete List (W/ examples), Dataquest. Available at: https://www.dataquest.io/blog/sql-commands/ (Accessed: 23 February 2026).
  3. Team, G.L.E. (2025). SQL commands (DDL, DML, DQL, DCL, TCL) with examples, Great Learning Blog: Free Resources what Matters to shape your Career! Available at: https://www.mygreatlearning.com/blog/sql-commands/ (Accessed: 23 February 2026).
  4. GeeksforGeeks (2025). SQL cheat sheet ( basic to advanced), GeeksforGeeks. Available at: https://www.geeksforgeeks.org/sql/sql-cheat-sheet/ (Accessed: 23 February 2026).

About the Author:

Thank you for reading the article! If you find this article useful, check out my other articles. Also, like, clap, comment, and follow me on Medium, LinkedIn, and my Homepage.

--

--

Bidyut Das
Bidyut Das

Written by Bidyut Das

0 followers

Writer focused on Education, Technologies, Fine Arts, and Indian Knowledge Systems. Sharing research-driven insights and interdisciplinary perspectives.