Search Jobs

Ticker

6/recent/ticker-posts

SQL Interview Question

Top 50 most asked SQL interview questions along with brief answers and examples:


1. What is SQL?

   - SQL stands for "Structured Query Language." It is a domain-specific language used for managing and querying relational databases.


2. Explain the difference between SQL and NoSQL databases.

   - SQL databases are relational, structured databases, while NoSQL databases are non-relational, unstructured, or semi-structured databases.


3. What is a DBMS (Database Management System)?

   - A Database Management System (DBMS) is software that provides tools for managing and interacting with databases. Examples include MySQL, Oracle, and SQL Server.


4. Define RDBMS (Relational Database Management System).

   - A Relational Database Management System (RDBMS) is a type of DBMS that organizes data into tables with rows and columns, providing relationships between tables. Examples include MySQL and PostgreSQL.


SQL Queries:


5. How do you retrieve all records from a table in SQL?

   - To retrieve all records, use the following SQL query:

   

  SELECT * FROM table_name;

 


6. What is the SQL command to retrieve unique values from a column?

   - To retrieve unique values from a column, use the DISTINCT keyword:

   

  SELECT DISTINCT column_name FROM table_name;

 


7. Explain the difference between WHERE and HAVING clauses in SQL.

   - The WHERE clause filters rows before aggregation, while the HAVING clause filters aggregated results. Example:

   

  SELECT department, AVG(salary) FROM employees

  WHERE salary > 50000

  GROUP BY department

  HAVING AVG(salary) > 60000;

 


8. How do you sort the result set in ascending and descending order?

   - Use the ORDER BY clause to sort. To sort in ascending order (default), use ASC, and for descending order, use DESC:

   Examples:


 SELECT column_name FROM table_name

 ORDER BY column_name ASC; -- or DESC for descending

 


9. Write an SQL query to count the number of records in a table.

   - Use the COUNT() function:

   

  SELECT COUNT(*) FROM table_name;

 


10. How can you select the top N records from a table?

- Use the LIMIT keyword (varies by database system):

    

   SELECT * FROM table_name

   LIMIT N;

  


11. What is a subquery in SQL, and why is it used?

- A subquery is a query nested inside another query. It is used to retrieve data that will be used in the main query. Example:

    

   SELECT name FROM customers

   WHERE id IN (SELECT customer_id FROM orders);

  


12. How do you perform a join operation in SQL?

- Use JOIN clauses to combine data from multiple tables based on a related column. Example of an INNER JOIN:

    

   SELECT orders.order_id, customers.name

   FROM orders INNER JOIN customers ON 

orders.customer_id = customers.customer_id;

  


SQL Functions:


13. What is the purpose of the GROUP BY clause in SQL?

- The GROUP BY clause is used to group rows with similar values in one or more columns, typically used with aggregate functions. Example:

    

   SELECT department, AVG(salary)

   FROM employees

   GROUP BY department;

  


14. How do you find the maximum and minimum values in a column using SQL?

- Use the MAX() and MIN() aggregate functions:

    

   SELECT MAX(column_name) FROM table_name;

   SELECT MIN(column_name) FROM table_name;

  


15. What is the difference between COUNT(*) and COUNT(column_name) in SQL?

- COUNT(*) counts all rows, while COUNT(column_name) counts non-NULL values in the specified column. Example:

    

SELECT COUNT(*) FROM customers; -- Counts all rows

SELECT COUNT(city) FROM customers; -- Counts non-NULL

values in the 'city' column

  


16. How do you use the SUM function in SQL?

- The SUM function calculates the sum of values in a column. Example:

    

   SELECT SUM(salary) FROM employees;


17. Explain the purpose of the AVG function in SQL.

   - The AVG function calculates the average value

of a column. Example:

    

   SELECT AVG(price) FROM products;

  


18. What is the difference between the DISTINCT and

UNIQUE keywords in SQL?


- DISTINCT is used in SELECT statements to return

unique rows, while UNIQUE is used as a

constraint to enforce unique values in a column.


19. How do you concatenate strings in SQL?

- Use the || operator in some databases, or the CONCAT function. Example:

 

   SELECT first_name || ' ' || last_name

FROM employees;

  


20. What is the DATEPART function used for in SQL?

- DATEPART extracts specific parts of a date or

time (e.g., year, month, day) from a datetime column.

Example:

 

   SELECT DATEPART(YEAR, order_date) FROM orders;

  


21. Explain the purpose of the COALESCE function in

SQL.

- The COALESCE function returns the first

non-NULL value in a list of expressions. Example:

 

   SELECT COALESCE(preferred_contact, 'N/A') FROM

customers;

  


SQL Constraints:


22. What is a primary key, and why is it important?

- A primary key uniquely identifies each record in

a table and enforces data integrity. It ensures

that no duplicate values are allowed in the

primary key column.



23. What is a foreign key, and how is it used in SQL?

- A foreign key is used to create relationships

between tables. It enforces referential integrity by

ensuring that

values in one table match values in another table's

primary key.



24. Explain the NOT NULL constraint in SQL.

- The NOT NULL constraint enforces that a

column cannot contain NULL values, ensuring data integrity.


25. What is a unique constraint, and when is it used?

- A unique constraint ensures that all values in a

column are unique (no duplicates). It is used to

prevent duplicate data.



26.What is the purpose of the CHECK constraint in SQL?

- The CHECK constraint is used to define a condition

that must be true for a record to be inserted or

updated.

It enforces data integrity based on a condition.


27. How do you add a default value to a column in SQL?

- Use the DEFAULT constraint to specify a default

value for a column. Example:

 

   CREATE TABLE employees (

       employee_id INT PRIMARY KEY,

       name VARCHAR(50),

       status CHAR(1) DEFAULT 'A'

   );

  


SQL Data Manipulation:


28. What is an INSERT statement, and how is it used?

- The INSERT statement is used to add new records

to a table. Example:

 

   INSERT INTO customers (first_name, last_name,

email)

   VALUES ('John', 'Doe', 'john.doe@email.com');

     


 



Post a Comment

0 Comments