InterviewSolution
| 1. |
Define Subqueries in SQL. Given a table of employees, write down a SQL query using subqueries to find all the employees with salaries greater than five thousand. |
|
Answer» A subquery in SQL is a query that is defined inside ANOTHER query to retrieve data or information from the database. The outer query of a subquery is referred to as the main query, while the inner query is referred to as the subquery. Subqueries are always PROCESSED first, and the subquery's RESULT is then passed on to the main query. It can be nested inside any query, including SELECT, UPDATE, and OTHER. Any comparison operators, such as >, or =, can be used in a subquery. The syntax of a subquery in SQL is given as follows: SELECT columnName [, columnName ]FROM tableOne [, tableTwo ]WHERE columnName OPERATOR (SELECT columnName [, columName ] FROM tableOne [, tableTwo ] [WHERE])A SQL query using subqueries to find all the employees with SALARIES GREATER than five thousand in the employee's table is given below: SQL> SELECT * FROM EMPLOYEES WHERE EMPLOYEE_ID IN (SELECT EMPLOYEE_ID FROM EMPLOYEES WHERE SALARY > 4500) ; |
|