


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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)