Common SQL Questions

How to update a table?

UPDATE table_name
SET col_1 = value_1, column_2 = value_2
WHERE condition;

627. Swap Salaryarrow-up-right : Write a solution to swap all 'f' and 'm' values (i.e., change all 'f' values to 'm' and vice versa) with a single update statement and no intermediate temporary tables.

update Salary
set sex = case
            when sex = 'f' then 'm'
            when sex = 'm' then 'f'
            else sex
        end;

How to delete a table from a database?

DROP TABLE table_name

How to add a column to a table?

ALTER TABLE table_name
ADD column_name datatype;

How to rename a column of a table?

How to find the nth highest value in a column of a table?

Using the OFFSET clause. For example, to find the 6th highest value from a column, we would use the following syntax:

OFFSET 5: Skip the first 5 rows of the result set.

Question 1: SQL Query to find the second highest salary of Employee(176. Second Highest Salaryarrow-up-right)

Question 2: SQL Query to find the third highest salary of Employee

Question 3: SQL query to find the Nth highest salary (177. Nth Highest Salaryarrow-up-right)

For example, given the above Employee table, the nth highest salary where N = 2 is 200. If there is no Nth highest salary, then the query should return null. You can see that we have used the above query to find the highest, second-highest, and third-highest salaries from the employee table.

Question 4: SQL Query to find Max Salary from each department.

These questions become more interesting if the Interviewer will ask you to print the department name instead of the department id, in that case, you need to join the Employee table with Department using the foreign key DeptID, make sure you do LEFT or RIGHT OUTER JOINarrow-up-right to include departments without any employee as well. Here is the query

Question 5: Finding duplicate emails (182. Duplicate Emailsarrow-up-right)

196. Delete Duplicate Emailsarrow-up-right: Write a solution to delete all duplicate emails, keeping only one unique email with the smallest id

Question 6: Find Customers Who Never Order

Question 7: Write an SQL Query to print the name of the distinct employee whose DOB is between 01/01/1960 to 31/12/1975.

Question 8: Write an SQL Query to find the number of employees according to gender whose DOB is between 01/01/1960 to 31/12/1975.

Question 9: Write an SQL Query to find the name of an employee whose name Start with ‘M’

Question 10: Find all Employee records containing the word "Joe", regardless of whether it was stored as JOE, Joe, or joe.

Question 11: There is a table which contains two columns Student and Marks, you need to find all the students, whose marks are greater than average marks i.e. list of above-average students.

Question 12: Write a SQL query to find the names of employees who have not been assigned to any project.

Question 13 : Create a Customer and Order Table and Connect Them

Question 14: How to get unique records from the table without using distinct keywords

Question 15: Write a SQL query to print the FIRST_NAME and LAST_NAME from Student table into single column COMPLETE_NAME.

Last updated