


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
How to use nested queries and aggregate functions in sql to solve complex database queries. Examples using a vip assets table and the employee database schema. Topics covered include the usage of exists, count, sum, max, min, and avg functions, as well as the group by and having statements.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



In this episode, we do a final, complicated case: Implementing DIVIDE in SQL Here’s an example problem: Find all the entities of type A that are associated with all memebrs of as set of type B. To apply this, lets use our employee database schema and rephrase the problem to be: EX: Find the first names of all employees who work in ALL projects in Brooklyn. We imagine two sets... the divisor and the dividend, which here will be B and A respectively. A: The set of all Employees B: The set of all Brooklyn projects What we want to say is “Select any employee who works on all the projects in Brooklyn” but this isn’t necessarily possible in SQL. There’s no way to really say “works on all”. Yet, it is easy to find all the projects that A works on. The way you do it in SQL, you compare the projects A works on with those in B, like this: SELECT A WHERE NOT EXISTS ( B - A.projects ) Here, A.projects represents all the projects A works on. This means that while you can’t just see if all projects in A are all in B, you CAN subtract the projects from B and see if any are left over. This may still be confusing, so it might be better to take a simpler database, illustrate it there, and then take it further to the employee table. Let’s start with a table about a VIP who wants to select his assets according to a specific set of parameters: ( please turn the page in your PDF )
The aggregate functions we’ll look at are: COUNT, SUM, MAX, MIN, AVG A B C D E 14 2 5 38 1 0 84 3 29 47 18 4 8 10 22 14 10 32 20 15 COUNT(A) on the above table would result in 4, since it doesn’t ignore dups (uh oh, slashdot!) and it also includes 0. NULL is a different story, but don’t consider it for now. In this introductory class, we’ll be avoiding NULL. Lets use an aggregate query in our Employee Database (sorry, no more VIP’s for now) EX: Find the max salary and min salary among employees who work for the Research dept. SELECT MAX(Salary), MIN(Salary) FROM E, D WHERE E.Dno = D.DNumber AND D.Dname = ‘Research’ GROUP in SQL Sometimes we want to run aggregates on specific parameters, such as counting the amount of employees on each project in the employee database. You simply add a clause at the end of the query to do this. Here’s how we’d do something like that in SQL, using the GROUP BY statement. EX: For each Project retrieve the pno and pname and number of employees that work on that project. SELECT P.Number, P.Pname, Count(W_O.essn) FROM W_O, P WHERE W_O.Pno = P.Pnumber GROUP BY P.Pnumber When we want to only select particular groups, we do an additional step. That is, you might think the WHERE clause would be used, but that’s incorrect, as the groups needs to be treated as separate from the rest of the results (since the groups are more or less laying on top of the query and cutting it into chunks). Instead, you use the HAVING keyword to ‘git r dun’, adding it to the end of the query. Since we didn’t get any specific examples of the ‘HAVING’ keyword, I guess I’ll have to make one up (uh oh!). SELECT SUM(Midget.isEvil), COUNT(Midget.Name) FROM Midget WHERE Midget.height < 4 GROUP BY Midget.homeCity HAVING Midget.Gender = ‘male’ (we need the “having” keyword because women are all evil, and no count is necessary for them)