SQL Query Interview Questions


 Q. Write a SQL Query to display all records from a table.

A. Select * from table_name;

Q. Write a SQL Query to display all records of employee whose name starts with ‘S’.

A. Select * from employee where emp_name like ‘S%’;

Q. Write a SQL query for printing the employee name from EMP table for alternate rows.

A. Even rows :

Select emp_id from (Select rowno, emp_Id from EMP) where mod(rowno,2)=0;

Odd rows :

Select emp_id from (Select rowno, emp_Id from EMP) where mod(rowno,2)=1;

Q. Write a SQL Query for printing the unique records from the table EMP.

A. Select distinct emp_id, emp_name from EMP;

Q. Write a SQL Query to display the employee id whose first name is ‘Vishal’ and second name is ‘Roy’.

A. Select emp_id from EMP where fir_name=’Vishal’ AND last_name=’Roy’;

Q. Write a SQL Query to fetch “emp_name” from EMP table In Upper Case.

A. Select upper(emp_name) from EMP;

Q. Write a SQL Query to print the first four characters of  emp_name from EMP table.

A. Select substring(emp_name,1,4) from EMP;

Q. Write a SQL query that fetches the unique values of DEPARTMENT from EMP table and prints its length.

A. Select distinct length(DEPARTMENT) from EMP;

Q. Write a SQL Query to print Employee id details from the EMP table order by emp_name in ascending order.

A. Select emp_id from EMP order by emp_name asc;

Q. Write an SQL Query to print all Employee details whose emp_name ends with ‘a’ and contains six alphabets.

A. Select * from EMP where emp_name like ‘_____a’;


Q. Write an SQL Query to print all Employee details whose emp_salary lies between 10,000 and 1,00,000.

A. Select * from EMP where emp_salary between 10,000 and 1,00,000;

Q. Write a SQL Query to print details of the EMP who have joined in Mar’2018.

A. Select * from EMP where year(join_date) = 2018 and month(join_date) = 3;

Q. Write a SQL Query to fetch common records of two tables.

A. Select * from Table1 INTERSECT Select * from Table2;

Q. Write a SQL Query to display the top 5 records of a table.

A. Select TOP 5 * from EMP order by emp_salary DESC;

Q. Write a SQL Query to show the last record from a table.

A. Select * from EMP where emp_id = (Select max(emp_id) from EMP);

Q. Write a SQL Query to fetch the names of Employee who have highest salary.

A. Select emp_name, emp_salary from EMP WHERE emp_salary = (Select max(emp_salary) from EMP);

Q. Write a SQL Query to find 2nd highest salary of an employee.

A. Select max(emp_salary) from EMP where emp_salary IN (SELECT emp_salary from EMP MINUS SELECT max(emp_salary) from EMP);

Q. Write a SQL Query to find 3rd highest salary of an employee.

A. Select emp_salary from EMP order by emp_salary desc limit 2,1;

In the above query, offset is set to two.

Q. Write a SQL Query to display the all rows(non-repeating) in two tables.

A. Select emp_name from EMP1 UNION SELECT emp_name from EMP2;

Q. Write a SQL Query to sort the employee’s lastname descending by their last name.

A. Select * from EMP order by emp_lastname DESC;

Q. Write a SQL Query to find the customers that have an OrderPrice value higher than the average OrderPrice value.

A. Select Customer from Orders where OrderPrice>(Select AVG(OrderPrice) from Orders);


No comments:

Post a Comment