Call: IN +91 7013283324 | +91 9347854179
(Click on WhatsApp Chat button)
Call: US +1 (415) 529-7271

Top 50 SQL Interview Questions Most Asked

Home  >>  Interview Questions  >>  Top 50 SQL Interview Questions Most Asked

Top 50 SQL Interview Questions Most Asked

On November 19, 2021, Posted by , In Interview Questions, With Comments Off on Top 50 SQL Interview Questions Most Asked

Structured Query Language, commonly known as SQL is the most popular language used to work with relational databases. SQL first came out nearly 50 years ago and since then, it has become a top name in the data management community. In this article, we will discuss fifty SQL questions with answers. 

Basic SQL questions

Question 1:  What is SQL?

Answer 1: SQL stands from Structured Query Language. It is a programming language that is used to manage relational databases.

Question 2: What are the uses of SQL?

Answer 2: SQL is used for:

  • creating databases.
  • creating and managing tables.
  • inserting, updating, and deleting data in tables.

Question 3: Name the subgroups in which SQL commands are divided.

Answer 3: There are four subgroups of SQL commands:

  • DDL
  • DML
  • DCL
  • TCL

Question 4: What is DDL?

Answer 4: DDL stands for Data Definition Language. These commands are used to define the structure of data. 

Question 5: Name all the DDL commands.

Answer 5: CREATE, ALTER, DROP, TRUNCATE, COMMENT, and RENAME.

Question 6: What is DML?

Answer 6: DML stands for Data Manipulation Language. These commands are used for manipulating data.

Question 7: Name all the DML commands.

Answer 7: SELECT, INSERT, UPDATE, DELETE, MERGE, CALL, EXPLAIN PLAN, and LOCK TABLE.

Question 8: What is DCL?

Answer 8: DCL stands for Data Control Language. These commands are used to give rights and permissions.

Question 9: Name all the DCL commands.

Answer 9: GRANT and REVOKE.

Question 10: What is TCL?

Answer 10 : TCL stands for Transaction Control Language. It is used to perform transactions on a database.

Question 11: Name all the TCL commands.

Answer 11: COMMIT, ROLLBACK, SAVEPOINT, and SET TRANSACTION.

Question 12: Which clause is used to specify conditions in a SQL query?

Answer 12: The WHERE clause is used to specify conditions in a SQL query.

Question 13: Suppose we have a table “Employees” with three columns – “name”, “age”, and “location”. Create a new table “NEWEMPLOYEES” and it should contain “name” and “age” of all the records from “Employees”.

CREATE TABLE NEWEMPLOYEES AS
SELECT  name, age FROM EMPLOYEES;

Question 14: Suppose we want to create a new table “NEWEMPLOYEES” from an existing table “EMPLOYEES”. We need to add all the records where “age” is greater than 30. How can we do it?

Answer 14: Execute the following query to create the required table.

      CREATE TABLE NEWEMPLOYEES AS

      SELECT  name, age FROM EMPLOYEES

      WHERE age > 30;

Question 15: What is the difference between primary key and unique key?

Answer 15: Following are the differences between the primary key and the unique key.

  • A table can only have one primary key while multiple unique keys can be present in a table.
  • The primary key cannot have a NULL value while the unique key can have a NULL value.
  • The primary key creates a clustered index while the unique key creates a non-clustered index.

Question 16: What is a foreign key?

Answer 16: A foreign key is a key that is used in SQL to link two tables. 

Question 17:  What is the use of the ALTER command?

Answer 17: The ALTER command is used to:

  • add, update, or delete the columns of a table.
  • add or remove the constraints.

Question 21: Suppose we have 20 records in the “EMPLOYEES” table. Each row has three columns – “name”, “age”, and “location”. The value of “location” can be the same for multiple records. Write a query to find all distinct values of “location” are there in the “EMPLOYEES” table.

Answer 21: SELECT DISTINCT location FROM EMPLOYEES

Question 22: Write a query to find the total number of employees who are older than 30 years in the “EMPLOYEES” table.

Answer 22: SELECT COUNT(name) FROM EMPLOYEES WHERE age > 30

Question 23: Write a query to find all the names from the “EMPLOYEES” table where the name starts with “a” and ends with “s”.

Answer 23: SELECT name from EMPLOYEES WHERE name LIKE “a%s”.

Question 24: Write a query to find all the records in the “EMPLOYEES” table where the location is either “New York”, “Chicago”, or “Detroit”.

Answer 24: SELECT * from EMPLOYEES WHERE location in (“New York”, “Chicago”, “Detroit”)

Question 25: Write a query to find all the records in the “EMPLOYEES” table where age is greater than 30 and location is “New York”.

Answer 25: SELECT * FROM EMPLOYEES WHERE age > 30 AND location = “New York”

Question 26: Which logical operators can be used in SQL queries?

Answer 26: AND, OR, and NOT.

Question 27: Write a query to get the first 5 records from the “EMPLOYEES” table.

Answer 27: SELECT * FROM EMPLOYEES LIMIT 2

Question 28: Write a query to count the number of records per location in the “EMPLOYEES” table.

Answer 28: SELECT COUNT(empid), location FROM EMPLOYEES GROUP BY location

Question 29: What is a join in SQL?

Answer 29: A join is a SQL clause that is used to combine multiple tables.

Question 30: Name the types of join in SQL.

Answer 30: Following are the types of joins in SQL.

  • INNER join
  • LEFT join
  • RIGHT join
  • OUTER join
  • SELF join
  • CROSS join

Suppose we have two tables:    

“EMPLOYEES” with columns “empid”, “name”, “age”, and “location”. 

“ONSITE” with columns “empid”, “name”, and “isAvailable”

Answer the following question according to the above tables.

Question 31: Write a query to find the names of the employees that exist in both tables.

Answer 31: SELECT EMPLOYEES.name from EMPLOYEES INNER JOIN ONSITE ON EMPLOYEES.empid = ONSITE.empid 

Question 32: Write a query to find the employee ids and names of the employees that exist in “EMPLOYEES” tables and their availability is true.

Answer 32: SELECT EMPLOYEES.empid, EMPLOYEES.name from EMPLOYEES INNER JOIN ONSITE ON EMPLOYEES.empid = ONSITE.empid WHERE ONSITE.isAvailable = true 

Question 33: What is the use of self join? Give an example.

Answer 33: A self join is used to join a table with itself. In the following example, a self join is used on the “EMPLOYEES” table. Following is the example of self join.

SELECT A.name AS NAME1, B.name AS NAME2 from EMPLOYEES A, EMPLOYEES B WHERE A.empid = B.empid

Others

Question 34:  Which operator is used to find the records in a given range?

Answer 34: The BETWEEN operator is used to find the records in a given range.

Question 35: What is the difference between IN and BETWEEN operators?

Answer 35: The BETWEEN operator is used to find the records by providing a range while the IN operator returns the records according to the specified values.

Question 36: Write a query to return all the records from the “EMPLOYEES” table but only if an employee name “Mark” whose age is 33 exists in the “ONSITE” table.

SELECT * FROM EMPLOYEES
WHERE EXISTS (
SELECT * FROM ONSITE WHERE name = "Mark" AND age = 33
)

Question 37: What is the use of ANY operator?

Answer 37: The ANY operator returns a boolean value. It is used to check if any of the given subqueries is meeting the condition.

Question 38: What is the use of ALL operator?

Answer 38: The ALL operator returns a boolean value. It is used to check if all of the given subqueries are meeting the condition.

Question 39: Along which clauses the ALL operator is used?

Answer 39: The ALL operator is used with SELECT, HAVING, and WHERE CLAUSE.

Question 40: When will the EXIST clause return true?

Answer 40: The EXIST clause returns true if the subquery returns at least one record. 

Question 41: Which keyword is used to sort columns?

Answer 41: The ORDER BY keyword is used for sorting columns.

Question 42: Which command is used to save the work?

Answer 42: COMMIT command

Question 43: If there was a mistake and you want to restore the database to the last commit, which command will you use?

Answer 43: ROLLBACK command

Question 44: Which operator is used to check empty values?

Answer 44: IS NULL command

Question 45: What are the different types of cursors in SQL?

Answer 45: SQL has two types of cursors.

  • Implicit cursors – created automatically
  • Explicit cursors – user-defined 

Question 46: Differentiate between SQL and NoSQL databases.

Answer 46: Following are the differences between SQL and NoSQL databases.

SQL databases are structured while NoSQL databases are non-structural. 

Tables are used in SQL while JSON or XML is used in NoSQL

Question 47: What is a view?

Answer 47: A view is a virtual representation of one or more tables in SQL. It does not exist physically in the database, rather it is just a representation of the data.

Question 48: Name the types of views in SQL.

Answer 48: There are four types of views.

  • Simple
  • Inline
  • Complex
  • Materialized

Question 49: Explain normalization. What are its advantages?

Answer 49: In SQL, the process of removing redundancy and duplication is known as normalization. Following are the advantages of normalization.

  • Better organization of data
  • Data access with better efficiency
  • Easy modification
  • Better tables 
  • Compact databases
  • Better flexibility

Question 50: Explain SQL injection.

Answer 50: SQL injection is a type of attack made by hackers to interfere in the SQL queries. These attacks are generally made on the input fields where the user is inputting any vulnerable information.

Comments are closed.