If you’re preparing for an SQL interview or just aiming to strengthen your knowledge, you’re in the right place. In this blog, we’ll cover fundamental SQL interview questions with practical answers and explanations that will boost your confidence. Let’s get started.
SQL (Structured Query Language) is the standard language used to communicate with relational databases. SQL allows you to query, update, insert, delete, and manage data stored in databases. Its primary purpose is to perform operations on data, ensuring you can extract meaningful insights and manage records.
For example, to fetch all data from a table, you’d use a query like –
SELECT * FROM table_name;
A common topic in interviews is the difference between SQL and NoSQL databases.
SQL Databases: These are relational and have a structured schema. Think of it as well-organized rows and columns, similar to Excel. SQL is perfect for applications that require complex queries, data integrity, and transactions (like banking).
NoSQL Databases: These are non-relational and can handle unstructured or semi-structured data, making them suitable for big data applications or real-time data, where flexibility is key (e.g., social media platforms).
In SQL, a data type specifies what kind of data a column can hold. Here’s a breakdown of common data types:
TRUE
or FALSE
values.The SELECT
statement retrieves data from a database. It’s the most commonly used SQL query.
SELECT column1, column2 FROM table_name;
Want to see everything in a table? Just use this –
SELECT * FROM employees;
How to Find the Number of Employees in Each City
SELECT city, COUNT(*) FROM employees GROUPBY city;
SELECT city, COUNT(*) FROM employees WHERE state = ‘CA’ GROUP BY city HAVING COUNT(*) > 5;
A JOIN
is used to combine data from two or more tables based on a related column.
NULL
values.LEFT JOIN
, returning all rows from the right.NULL
values are returned for columns from the right table.To join two tables, for example, employees and departments –
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
To update records in a table:
UPDATE employees SET salary = 60000 WHERE employee_id = 101;
The ORDER BY clause sorts the result set based on one or more columns, either in ascending (ASC) or descending (DESC) order
SELECT * FROM employees ORDER BY salary DESC;
DROP: Removes the entire table and its data.
DELETE: Removes specific rows from a table.
TRUNCATE: Removes all rows from a table but keeps the table structure intact.
TRUNCATE employees:
To backup a MySQL database:
mysqldump -u username -p database_name > backup.sql;
To restore a MySQL database:
mysql -u username -p database_name < backup.sql;
A PRIMARY KEY uniquely identifies each record in a table. It ensures that no duplicate rows exist, and each record can be uniquely retrieved.
A FOREIGN KEY links records in one table to a PRIMARY KEY
in another table. It establishes a relationship between two tables, enforcing referential integrity.
An INDEX helps speed up the retrieval of rows by creating a quick lookup reference for specific columns. However, indexes can slow down INSERT
and UPDATE
operations as they need to be updated as well.