Join Subquery
Join Subquery
Inner join
Left join
Right join
Full outer join
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;
Use Join Operations
1. Show the employee_name and his job_title.
3. Write a query in SQL to display the department name, city, and state province for each
department.
4. Write a query in SQL to display the first name, last name, department number and name,
6. Write a query in SQL to display the department name and number of employees in each of
Subquery Operations
(Scalar Subquery, Column Subquery, Row Subquery, Correlated Subquery, Derived
table)
a) A subquery can return a scalar (a single value), a single row (multi-column), a single column (multi-
row), or multi-row multi-column (derived table).
b) A subquery can contain many of the keywords that an ordinary SELECT can contain:
DISTINCT, GROUP BY, ORDER BY, LIMIT, joins, UNION constructs, functions, and so on.
c) A subquery's outer statement can be any one of: SELECT, INSERT, UPDATE, DELETE, SET etc.
d) A subquery must always appear within parentheses
1. Scalar Subquery:
▪ A subquery is a scalar subquery that returns a single value.
▪ A scalar subquery is a simple operand, and you can use it almost anywhere a
single column value or literal is legal.
▪ If the subquery result is empty, the result is NULL.
Example 1: Show the employee id, salary for only those employees having
greater salary than the employee id 150.
Also show the salary of employee id 150 with each employee record.
Example 2: Show all the employee details for only those employees receiving
salary higher than the average salary of all the employees. Also show the
average salary of all employees with each employee record.
Example 3: Show those employee details, average salary of his own department
who receives higher salary then the average salary of his department
2. Column Subquery: When the subquery returns a Single Column but multiple rows.
Use operators:
▪ ANY – return TRUE if the comparison is TRUE for ANY of the values in the column that
the subquery returns.
▪ ALL – return TRUE if the comparison is TRUE for ALL of the values in the column that
the subquery returns.
▪ IN – it is equivalent to ( = ANY ) operator.
▪ SOME – it is equivalent to ANY operator
Example 4(ALL operator): Show those employee details for only those employees
receiving salary higher than all other employee salaries of department number 50.
Also show the highest salary of department number 50 with each employee record.
Example 5(ANY operator): Show those employee details for only those employees
receiving salary higher than any of the employee salaries of department no 50. Also
show the lowest salary of department no 50 with each employee record
3. Row Subquery: A row subquery is a subquery variant that returns a single row
and can thus return more than one column value.
Example 1: Show those employees employee id, first name, job id, department id, job id of
employee number 150, department id of employee number 150 who works in the same
department and same job type as employee having employee number 150.
4. Derived Table: A derived table is an expression that generates a table within
the scope of a query FROM clause. A subquery in a SELECT statement FROM
clause is a derived table.
Example 1: Show the maximum value of the department wise total salary.
5. Correlated Subquery: A correlated subquery is a subquery that contains a
reference to a table that also appears in the outer query.
Example 2: Show those employee details receiving highest salary in his job type