



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of sql select statements, focusing on the use of aliases to rename columns or tables, self-joins to combine a table with itself, and subqueries to nest one query inside another. The examples given illustrate the use of aliases and self-joins to find employees working in the same department and subqueries to search for employees in a specific department or with a specific name.
Typology: Slides
1 / 5
This page cannot be seen from the preview
Don't miss anything!
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
SELECT A.Name FROM Employee A, Employee B
WHERE A.Dept=B.Dept
AND B.Name=‘Andy’
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
A.Name A.Dept B.Name B.Dept John Marketing John Marketing Mary Sales John Marketing Peter Sales John Marketing Andy Marketing John Marketing Anne Marketing John Marketing John Marketing Mary Sales Mary Sales Mary Sales Peter Sales Mary Sales Andy Marketing Mary Sales Anne Marketing Mary Sales
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
A.Name A.Dept B.Name B.Dept John Marketing John Marketing Andy Marketing John Marketing Anne Marketing John Marketing Mary Sales Mary Sales Peter Sales Mary Sales Mary Sales Peter Sales Peter Sales Peter Sales John Marketing Andy Marketing Andy Marketing Andy Marketing Anne Marketing Andy Marketing
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
A.Name A.Dept B.Name B.Dept John Marketing Andy Marketing Andy Marketing Andy Marketing Anne Marketing Andy Marketing
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
A.Name John Andy Anne
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
SELECT Name FROM Employee WHERE Dept = (SELECT Dept FROM Employee WHERE Name=‘Andy’)
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
SELECT Name FROM Employee WHERE Dept = ‘Marketing’
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
Employee Name Department Manager John Marketing Chris Mary Marketing Chris Chris Marketing Jane Peter Sales Jane Jane Management (^) Name Department Manager
Chris Marketing Jane Jane Management
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
SELECT Name FROM Employee WHERE Salary >= ALL ( SELECT Salary FROM Employee)
Name Salary Mary 20, John 15, Jane 25, Paul 30,
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
Name Salary Mary 20, John 15, Jane 25, Paul 30,
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
SELECT * FROM Items WHERE itmID IN ( SELECT itmID FROM ItemKey WHERE keyID IN ( SELECT keyID FROM Keywords WHERE keyWord LIKE 'word1%')) AND itmID IN ( SELECT itmID FROM ItemKey WHERE keyID IN ( SELECT keyID FROM Keywords WHERE keyWord LIKE 'word2%'))
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
Track
cID Num Title Time aID
1 1 Violent 239 1 1 2 Every Girl 410 1 1 3 Breather 217 1 1 4 Part of Me 279 1 2 1 Star 362 1 2 2 Teaboy 417 2
CD cID Title Price 1 Mix 9. 2 Compilation 12.
Artist aID Name 1 Stellar 2 Cloudboy
More SQL SELECT G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS
Find a list of the names of those artists who have a track on the CD with the title “Compilation”. (4 marks)
Note, this is one of the questions from last time, but there are alternative solutions using subqueries, try solving this with a query where you never list more than one table for a single SELECT statement