DEV Community

Guru prasanna
Guru prasanna

Posted on

SQL Interview questions and answers

SQL Interview Questions & Answers:

1. What is SQL?

--> SQL (Structured Query Language) is used to store, retrieve, manage, and manipulate data in relational databases like MySQL, PostgreSQL, SQL Server, and Oracle.

2. What are the different types of SQL commands?

SQL commands are categorized into five types:

  • DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE (Defines database structure).
  • DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE (Manipulates data).
  • DCL (Data Control Language): GRANT, REVOKE (Controls user permissions).
  • TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT (Manages transactions).
  • DQL (Data Query Language): SELECT (Retrieves data).

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

  • DELETE removes specific rows from a table and can be rolled back using ROLLBACK.
  • TRUNCATE removes all rows from a table but keeps its structure. It cannot be rolled back.
  • DROP removes the entire table, including its structure. It also cannot be rolled back.

4. What is the difference between WHERE and HAVING?

WHERE is used to filter rows before grouping.
HAVING is used to filter grouped data after GROUP BY.

Example:

SELECT department, COUNT(*) 
FROM employees 
WHERE salary > 30000  
GROUP BY department
HAVING COUNT(*) > 5;  
Enter fullscreen mode Exit fullscreen mode

5. What is a Primary Key and Foreign Key?

--> Primary Key uniquely identifies each record in a table. It cannot have duplicate or NULL values.
--> Foreign Key references the primary key of another table, maintaining relationships between tables.

Example:

CREATE TABLE Customers (
    customer_id INT PRIMARY KEY,
    name VARCHAR(100)
);
Enter fullscreen mode Exit fullscreen mode
CREATE TABLE Orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);
Enter fullscreen mode Exit fullscreen mode

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

  • UNION combines results from multiple queries and removes duplicates.
  • UNION ALL combines results without removing duplicates.

Example:

SELECT name FROM customers
UNION  
SELECT name FROM employees;

SELECT name FROM customers
UNION ALL 
SELECT name FROM employees;
Enter fullscreen mode Exit fullscreen mode

7. What are Joins in SQL? Explain types.

Joins combine records from multiple tables based on a related column.

  • INNER JOIN: Returns matching records from both tables.
  • LEFT JOIN: Returns all records from the left table + matching records from the right table.
  • RIGHT JOIN: Returns all records from the right table + matching records from the left table.
  • FULL JOIN: Returns all records when there is a match in either table.
  • SELF JOIN: Joins a table with itself.
  • CROSS JOIN: Returns the Cartesian product of both tables.

Example of INNER JOIN:

SELECT customers.name, orders.order_id  
FROM customers  
INNER JOIN orders ON customers.customer_id = orders.customer_id;
Enter fullscreen mode Exit fullscreen mode

8. What is an Index in SQL?

--> An index improves search performance by creating a lookup table for faster access.
--> However, it can slow down INSERT, UPDATE, and DELETE operations because the index must be updated.

Example:

CREATE INDEX idx_customer_name ON customers(name);
Enter fullscreen mode Exit fullscreen mode

9. What are Aggregate Functions?

Aggregate functions perform calculations on multiple rows and return a single value.

COUNT() → Returns the number of rows.
SUM() → Returns the total sum.
AVG() → Returns the average value.
MIN() → Returns the smallest value.
MAX() → Returns the largest value.
Enter fullscreen mode Exit fullscreen mode

Example:

SELECT department, COUNT(*) AS total_employees, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

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

GROUP BY groups rows with the same values and is used with aggregate functions.
ORDER BY sorts the result set in ascending (ASC) or descending (DESC) order.
Enter fullscreen mode Exit fullscreen mode

Example:

SELECT department, COUNT(*) 
FROM employees 
GROUP BY department 

SELECT name, salary 
FROM employees 
ORDER BY salary DESC;
Enter fullscreen mode Exit fullscreen mode

11. What is a Subquery?

A subquery is a query inside another query, typically used in WHERE, FROM, or SELECT clauses.

Example:

SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
Enter fullscreen mode Exit fullscreen mode
  • Executes the inner query first (AVG(salary)).
  • The outer query filters employees with a salary above the average.

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay