The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 3 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Could someone check my SQL lab work? I would certainly appreciate it.Â
Thanking you in advance.
1.
Â
SELECTemp_ssn,emp_last_name,emp_dpt_number,emp_salary AS [Max Department Salary]
FROMemployeeAS [e1]
WHEREemp_salary=(
               SELECTMAX(emp_salary)
               FROMemployeeAS [e2]
               WHERE e2.emp_dpt_number = e1.emp_dpt_number)
ORDERBYemp_dpt_numberASC;
Â

Â
2.
Â
SELECTemp_dpt_number,MAX(emp_salary)AS [Top Salary]
FROM employeeÂ
GROUPBYemp_dpt_number
ORDERBYemp_dpt_numberASC;
Â

While you can get the top salaries from each department, you cannot get it in a format which would allow you to include the social security number or employee last name due to those being UNIQUE. If you include the employee social security number and employee name in the above SQL, you will get the following:
Â
SELECTemp_ssn,emp_last_name,emp_dpt_number,MAX(emp_salary)AS [Top Salary]
FROM employeeÂ
GROUPBYemp_dpt_number
ORDERBYemp_dpt_numberASC;
Â
Msg 8120, Level 16, State 1, Line 1
Column 'employee.emp_ssn' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Â
Therefore, the use of Grouping and Aggregation while only get you so much information but not in the format requested.
Â
3.
Â
SELECTwork_emp_ssn,work_pro_number,work_hoursAS [Highest Work Hours]
FROMassignmentAS [a1]
WHEREwork_hours=(
               SELECTMAX(work_hours)
               FROMassignmentAS [a2]
               WHERE a2.work_pro_number = a1.work_pro_number)
ORDERBYwork_hoursDESC;
Â
Â

Â
Â
4.
Â
SELECTwork_emp_ssn,CONCAT(emp_first_name,' ',emp_last_name)AS [Name],emp_dpt_number,work_pro_number,work_hoursAS [Highest Work Hours] Â
FROM assignment AS [a1] JOIN employee AS [e] ON (e.emp_ssn= a1.work_emp_ssn)
WHEREwork_hours=(
               SELECTMAX(work_hours)Â
               FROMassignmentAS [a2]Â
               WHERE a2.work_pro_number = a1.work_pro_number)
ORDERBYwork_hoursDESC;
Â

Â
Â
Â
Â
Â
Â
Â
Â
5.
Â
SELECTemp_last_nameAS [Last Name],emp_first_nameAS [First Name]
FROM employee
WHEREemp_ssnIN
  (SELECTwork_emp_ssnÂ
   FROM assignment
   WHEREwork_hours>= 8 ANDwork_pro_numberINÂ
    (SELECTpro_number
     FROM project
     WHEREpro_name='Payroll'));
Â

Â
6.
Â
SELECTdpt_nameAS [Department Name],pro_numberAS [Project Number],pro_nameAS [Project Name],pro_locationAS [Project Location]
FROM department dINNERJOIN project p ON (d.dpt_no=pro_dept_number)
WHEREpro_location='Edwardsville'
Â

Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
SELECTemp_last_name,emp_first_name,work_pro_number,work_hours
FROM employee e LEFTOUTERJOIN assignment a ON (e.emp_ssn=a.work_emp_ssn)
ORDERBYemp_last_name
Â

Â
Â
8.
Â
SELECTemp_last_name,emp_first_name,work_pro_number,work_hours
FROM employee e LEFTOUTERJOIN assignment a ON (e.emp_ssn=a.work_emp_ssn)
WHEREwork_hoursISNULL
ORDERBYemp_last_name
Â

Â
Â
Â
Â
Â
Â
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly