What are sub-queries?
Sub queries are select statements that appear as part of another SQL statement. A sub query is often used when comparing data from different tables. It is written in the WHERE clause, and must be surrounded by brackets so that it cannot be confused with its parent query.
SELECT first_name, last_name
FROM employees
WHERE department_id=(SELECT department_id
FROM department
WHERE section_id=1112);
The above example lists the first name and last name of all the employees from section 1112. The section ID field is not in the employee table but the department ID field is. The section ID is in the department table, so this table is used to find a matching department ID that can be used to query the employee table.
SELECT first_name, last_name
FROM employee
WHERE employee_id=(SELECT employee_id
FROM managers);
This sub query lists employees whom exist in both the employees and managers tables.
Back
Sponsored links
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Villanova University Six Sigma & IT Certificate Programs
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!
Key Elements to an Effective Business Continuity Plan
Learn to develop a plan that clarifies what is critical and sets specific recovery requirements.
Learn to develop a plan that clarifies what is critical and sets specific recovery requirements.
Web based bug tracking - AdminiTrack.com
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.
