SlideShare a Scribd company logo
1 of 2
SA0932A

SQL Lab 2 – Nested queries and set operations (SQL Server and
embedding in CF/CFScript/PHP)
We are using the same database for this lab so you should be WELL familiar with it by now!

PERSONNEL
SNU                                  MANAGER       SE    JOINDAT       DIV    SALAR     BONU
M         SURNAME    JOBTITLE        *             X     E             *      Y         S
 3016     BROCK      SECRETARY           3414      F       11-Jul-83     10     12288
 3107     HAMILTON   SALESMAN            3200      M      13-Dec-82      20     18534     4500
 3200     RAINES     MANAGER             3812      F       01-Jul-80     20     25872      500
          TRINGHA
  3215    M          CLERK                  3200   M      16-Mar-81     20       9384
                     ACCOUNTAN
  3365    HUGHES     T                      3414   M     17-Nov-83      10      23760
  3407    MACRAE     SECRETARY              3926   F     13-Dec-82      10      16200
  3414    WELHAM     MANAGER                3813   M     22-Jun-87      10      25872
  3488    STYLES     CLERK                  3365   F     01-Dec-82      10       8652
  3698    FRENCH     CONSULTANT             3788   M     01-Apr-80      30      20184        0
  3724    ADYE       CONSULTANT             3788   M     28-Oct-69      30      29400      825
  3788    KUMAR      MANAGER                3813   M     10-Feb-75      30      30816     1586
  3812    KHAN       DIRECTOR               3926   M     14-Dec-82      20      42000      950
  3813    BATE       DIRECTOR               3926   F     20-Apr-70      10      46752
  3926    TOWLSON    CHAIRMAN                      F     03-Sep-84      10      45816
  3989    BRAY       CONSULTANT             3788   M     24-Sep-73      30      18000        825


BRANCH                                   TRANSPORT
DI                                       SNUM                            MODE       MILEAG      DIV
V     DIVNAME        CITY                *         REGNO      MAKE       L          E           *
 10   ADMIN          LONDON                3107    G343ALJ    BMW            316I      27216      20
 20   SALES          BRISTOL                                               SIERR
 30   CONSULTING     LONDON                3200    F260PQN    FORD              A       30986      20
 40   TRAINING       MANCHESTER            3414    E329WUF    VOLVO           340       28216      10
 50   COMPUTING      BIRMINGHAM                                            SIERR
                                           3698    G181BSS    FORD              A       18311      30
                                           3724    G213CBS    FORD        ORION         20016      30
                                           3788    G268JFN    VOLVO           440       16485      30
                                                              TOYOT
                                           3812    H989TNP    A           LEXUS          2193      20
                                           3813    F138RUS    BMW            320        28726      10
                                           3926    G298ALM    JAGUAR         XJ6        21817      10
                                           3989    E156EBE    FORD        ORION         30168      30




 For all of these questions, feel free to implement them in SQL Server and/or
 embedded in CF/CFScript/PHP. Just vary what you do so that you remain practiced.


Exercise 1 – Use Set operations
a) Find the division numbers that currently have no staff in them
b) Find the branch numbers outside LONDON who use FORD cars.



Sa0932a                             21/10/2010
Exercise 2 – Nested Queries
For some of these queries, set operations will also come in handy. You may be able to do
several of these queries without nesting, or using several different nested methods.

a) List the name, job title and salary of employees who have the same job title as Bray.
b) Find the employees in division 10 that have a job title that is the same as anyone in the
   sales division. (Hint: You could first do a query that Find all employees in division 10 that
   have the same job title as anyone in division 30, then adapt it).
c) Find the employees located in London who have the same job title as Raines. Return
   the results in alphabetical order of employee name.
d) Find each division’s average annual salary for all its employees except for the managers
   and chairman.
e) List all employees who have been hired before their managers.
f) Does anyone earn more than their manager? If so, who?
g) List the job title groups that have an average salary greater than the average salary of
   managers.
h) List the job title(s) and employee(s) with the highest salary in every division

   Hint: complete the suggested solution below

   select surname,jobtitle,salary from personnel p where salary = (select _____ from
   personnel p2 where __________);



Exercise 3: These queries may be real puzzles – or they may be surprisingly easy
a) Find the division with the highest average salary. List the division’s name, number and
   average salary. (hint: use TOP to get the highest – look it up in Google)
b) Calculate the daily pay (including bonus) and the hourly rate of employees in Kumar’s
   division. Assume that there are 12 months in a year, 22 working days in a month and 8
   working hours in a day. {hint: use the NULLIF function to convert nulls to zeros for the
   bonus i.e. NULLIF(Bonus,0)}
c) Find the 3 staff with the highest salary. (Hint: use TOP again)
d) Can you think of three or even four different methods to find the staff who do not have a
   car?
e) Find the average salary of all staff who manage other staff. Note that you cannot use
   the jobtitle column – e.g. Khan is a director but does manage Raines.
   It is then quite easy to adapt the query to find the average salary of all staff who do NOT
   manage other staff.
f) Which divisions have more male than female staff, and how many more? (The answer is
   div 20 and 30, with 2 and 4 more respectively).




Sa0932a                                21/10/2010

More Related Content

Viewers also liked

A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginnersRam Sagar Mourya
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questionsPyadav010186
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands1keydata
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answersvijaybusu
 

Viewers also liked (10)

Sql queries
Sql queriesSql queries
Sql queries
 
Dbms Lecture Notes
Dbms Lecture NotesDbms Lecture Notes
Dbms Lecture Notes
 
Dbms viva questions
Dbms viva questionsDbms viva questions
Dbms viva questions
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
 
DBMS Practical File
DBMS Practical FileDBMS Practical File
DBMS Practical File
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 

Similar to Sql tutorial

Massey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manualMassey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manualfjekkdmmem
 
Massey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manualMassey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manualfjekkdmmem
 
Massey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manualMassey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manualfjjskekdm
 
Massey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manualMassey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manualfjjskekdm
 
Massey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualMassey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualfhsjejkdmem
 
Massey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualMassey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualfhsjejkdmem
 
Massey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualMassey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualfjsekdhjenmdn
 
Massey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualMassey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualfjsekdhjenmdn
 
Massey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manualMassey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manualfjekkdmmem
 
Massey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manualMassey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manualfjjskekdm
 
Massey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manualMassey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manualfjjskekdm
 
Massey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manualMassey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manualfjekkdmmem
 
Massey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manualMassey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manualfjjfyjsekdmme
 
Massey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manualMassey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manualfjjfyjsekdmme
 
Massey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manualMassey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manualfjjfyjsekdmme
 
Massey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manualMassey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manualfjjfyjsekdmme
 
Massey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualMassey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualfjskekdmmse
 
Massey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualMassey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualfjskekdmmse
 
Massey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualMassey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualofpsdjksemmds
 
Massey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualMassey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualofpsdjksemmds
 

Similar to Sql tutorial (20)

Massey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manualMassey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manual
 
Massey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manualMassey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manual
 
Massey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manualMassey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manual
 
Massey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manualMassey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manual
 
Massey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualMassey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manual
 
Massey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualMassey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manual
 
Massey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualMassey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manual
 
Massey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualMassey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manual
 
Massey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manualMassey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manual
 
Massey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manualMassey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manual
 
Massey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manualMassey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manual
 
Massey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manualMassey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manual
 
Massey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manualMassey ferguson mf 815 combines parts catalogue manual
Massey ferguson mf 815 combines parts catalogue manual
 
Massey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manualMassey ferguson mf 810 combines parts catalogue manual
Massey ferguson mf 810 combines parts catalogue manual
 
Massey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manualMassey ferguson mf 835 combines parts catalogue manual
Massey ferguson mf 835 combines parts catalogue manual
 
Massey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manualMassey ferguson mf 830 combines parts catalogue manual
Massey ferguson mf 830 combines parts catalogue manual
 
Massey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualMassey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manual
 
Massey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualMassey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manual
 
Massey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manualMassey ferguson mf 825 combines parts catalogue manual
Massey ferguson mf 825 combines parts catalogue manual
 
Massey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manualMassey ferguson mf 820 combines parts catalogue manual
Massey ferguson mf 820 combines parts catalogue manual
 

Recently uploaded

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 

Recently uploaded (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

Sql tutorial

  • 1. SA0932A SQL Lab 2 – Nested queries and set operations (SQL Server and embedding in CF/CFScript/PHP) We are using the same database for this lab so you should be WELL familiar with it by now! PERSONNEL SNU MANAGER SE JOINDAT DIV SALAR BONU M SURNAME JOBTITLE * X E * Y S 3016 BROCK SECRETARY 3414 F 11-Jul-83 10 12288 3107 HAMILTON SALESMAN 3200 M 13-Dec-82 20 18534 4500 3200 RAINES MANAGER 3812 F 01-Jul-80 20 25872 500 TRINGHA 3215 M CLERK 3200 M 16-Mar-81 20 9384 ACCOUNTAN 3365 HUGHES T 3414 M 17-Nov-83 10 23760 3407 MACRAE SECRETARY 3926 F 13-Dec-82 10 16200 3414 WELHAM MANAGER 3813 M 22-Jun-87 10 25872 3488 STYLES CLERK 3365 F 01-Dec-82 10 8652 3698 FRENCH CONSULTANT 3788 M 01-Apr-80 30 20184 0 3724 ADYE CONSULTANT 3788 M 28-Oct-69 30 29400 825 3788 KUMAR MANAGER 3813 M 10-Feb-75 30 30816 1586 3812 KHAN DIRECTOR 3926 M 14-Dec-82 20 42000 950 3813 BATE DIRECTOR 3926 F 20-Apr-70 10 46752 3926 TOWLSON CHAIRMAN F 03-Sep-84 10 45816 3989 BRAY CONSULTANT 3788 M 24-Sep-73 30 18000 825 BRANCH TRANSPORT DI SNUM MODE MILEAG DIV V DIVNAME CITY * REGNO MAKE L E * 10 ADMIN LONDON 3107 G343ALJ BMW 316I 27216 20 20 SALES BRISTOL SIERR 30 CONSULTING LONDON 3200 F260PQN FORD A 30986 20 40 TRAINING MANCHESTER 3414 E329WUF VOLVO 340 28216 10 50 COMPUTING BIRMINGHAM SIERR 3698 G181BSS FORD A 18311 30 3724 G213CBS FORD ORION 20016 30 3788 G268JFN VOLVO 440 16485 30 TOYOT 3812 H989TNP A LEXUS 2193 20 3813 F138RUS BMW 320 28726 10 3926 G298ALM JAGUAR XJ6 21817 10 3989 E156EBE FORD ORION 30168 30 For all of these questions, feel free to implement them in SQL Server and/or embedded in CF/CFScript/PHP. Just vary what you do so that you remain practiced. Exercise 1 – Use Set operations a) Find the division numbers that currently have no staff in them b) Find the branch numbers outside LONDON who use FORD cars. Sa0932a 21/10/2010
  • 2. Exercise 2 – Nested Queries For some of these queries, set operations will also come in handy. You may be able to do several of these queries without nesting, or using several different nested methods. a) List the name, job title and salary of employees who have the same job title as Bray. b) Find the employees in division 10 that have a job title that is the same as anyone in the sales division. (Hint: You could first do a query that Find all employees in division 10 that have the same job title as anyone in division 30, then adapt it). c) Find the employees located in London who have the same job title as Raines. Return the results in alphabetical order of employee name. d) Find each division’s average annual salary for all its employees except for the managers and chairman. e) List all employees who have been hired before their managers. f) Does anyone earn more than their manager? If so, who? g) List the job title groups that have an average salary greater than the average salary of managers. h) List the job title(s) and employee(s) with the highest salary in every division Hint: complete the suggested solution below select surname,jobtitle,salary from personnel p where salary = (select _____ from personnel p2 where __________); Exercise 3: These queries may be real puzzles – or they may be surprisingly easy a) Find the division with the highest average salary. List the division’s name, number and average salary. (hint: use TOP to get the highest – look it up in Google) b) Calculate the daily pay (including bonus) and the hourly rate of employees in Kumar’s division. Assume that there are 12 months in a year, 22 working days in a month and 8 working hours in a day. {hint: use the NULLIF function to convert nulls to zeros for the bonus i.e. NULLIF(Bonus,0)} c) Find the 3 staff with the highest salary. (Hint: use TOP again) d) Can you think of three or even four different methods to find the staff who do not have a car? e) Find the average salary of all staff who manage other staff. Note that you cannot use the jobtitle column – e.g. Khan is a director but does manage Raines. It is then quite easy to adapt the query to find the average salary of all staff who do NOT manage other staff. f) Which divisions have more male than female staff, and how many more? (The answer is div 20 and 30, with 2 and 4 more respectively). Sa0932a 21/10/2010