MySQL Subquery
MySQL Subquery
What is Subquery?
• A Subquery or Inner query or a Nested query is a query within
another SQL query (main query)
• Subqueries can return individual values or a list of records
• Subqueries must be enclosed with parenthesis
• Usually located in WHERE or HAVING clause
• Subqueries can be used with the following SQL statements along with
the comparison operators like !=, <>, =, <, >, >=, <=, IN, NOT IN
What is Subquery?
MULTIPLE-COLUMN SUBQUERY
Where do we start? FINAL RESULT
• Subquery will be execute first, 4 R3 MAIN
QUERY
result from subquery will be
passed to the main query for 3 R2
further process
• When reading or writing SQL
2 R1
subqueries, you should start from SUBQUERIES
the bottom upwards, working out
which data is to be passed to the 1
next query up.
Questions
1. Display employee number, first name, last name and salary for any
employees whose salary is greater than average salary
2. Display theme park(s) which do not hire any employee
3. Which theme park(s) do not register any attraction places in the
database?
ROLLBACK VS COMMIT
select * from employee where emp_num = 106;
set autocommit = 0;
update employee set emp_fname = 'test', emp_lname = 'test' where
emp_num = 106;
select * from employee where emp_num = 106;
rollback;
select * from employee where emp_num = 106;
ROLLBACK VS COMMIT
select * from employee where emp_num = 106;
set autocommit = 0;
update employee set emp_fname = 'Gemma', emp_lname = 'Smith'
where emp_num = 106;
select * from employee where emp_num = 106;
commit;
rollback;
select * from employee where emp_num = 106;