I need help with a few SQL database codes to return some resutls! The queries th
ID: 3606197 • Letter: I
Question
I need help with a few SQL database codes to return some resutls! The queries that I need to write are described below this and the tables I have are described below the questions.
--------------------
Now this is the difficult part for me, what would I need to input as the queries to get the results below?
*Find the ssn and last name of every employee whose ssn contains two consecutive 8's, and has a supervisor. Sort the results by ssn.
*For every employee who works for more than 20 hours on any project that is controlled by the research department: Find the ssn, project number, and number of hours. Sort the results by ssn.
*Write a query that consists of one block only.
For every employee who works less than 10 hours on any project that is controlled by the department he works for: Find the employee's lname, his department number, project number, the number of the department controlling it, and the number of hours he works on that project. Sort the results by lname.
*For every employee who works on any project that is located in Houston: Find the employees ssn and lname, and the names of his/her dependent(s) and their relationship(s) to the employee. Notice that there will be one row per qualyfing dependent. Sort the results by employee lname.
*Write a query that consists of one block only.
For every employee who works for a department that is different from his supervisor's department: Find his ssn, lname, department number; and his supervisor's ssn, lname, and department number. Sort the results by ssn.
*Find pairs of employee lname's such that the two employees in the pair work on the same project for the same number of hours. List every pair once only. Sort the result by the lname in the left column in the result.
*For every employee who has more than one dependent: Find the ssn, lname, and number of dependents. Sort the result by lname
*For every project that has more than 2 employees working on and the total hours worked on it is less than 40: Find the project number, project name, number of employees working on it, and the total number of hours worked by all employees on that project. Sort the results by project number.
*For every employee whose salary is above the average salary in his department: Find the dno, ssn, lname, and salary. Sort the results by department number.
*For every employee who works for the research department but does not work on any one project for more than 20 hours: Find the ssn and lname. Sort the results by lname
*For every employee who works on every project that is controlled by department 4: Find the ssn and lname. Sort the results by lname
-----------------
Tables
-
employee, department, dept_locations, project, works_on, dependent
-
Keys in order of tables
-
fname, lname, ssn, bdate, address, sex, super_ssn, dno
dnumber, dname, mgrssn, mgrstartdate
dnumber, dlocation
pname, pnumber, plocation, dnum
essn, pno, hours
essn, dependent_name, sex, bdate, relationship
Explanation / Answer
SQL Queries for the first 4 questions mentioned are as follows:
1) SELECT ssn, lname FROM employee e WHERE ssn LIKE '%88%' AND super_ssn IS NOT NULL SORT BY ssn
2) SELECT e.ssn, p.pnumber, w.hours FROM employee e LEFT JOIN department d ON (e.dno = d.dnumber AND d.name = "research") LEFT JOIN project p ON (d.dnumber = p.dnum) LEFT JOIN work_on w (p.pnumber = w.pno) WHERE w.hours > 20 SORT BY ssn
3) SELECT e.lname, e.dno, p.pnumber, COUNT(DISTINCT d.dname), w.hours FROM employee e LEFT JOIN department d ON (e.dno = d.dnumber) LEFT JOIN project p ON (d.dnumber = p.dnum) LEFT JOIN work_on w (p.pnumber = w.pno) WHERE w.hours < 10 SORT BY lname
4) SELECT e.ssn, e.lname, dep.dependent_name, dep.relationship FROM employee e LEFT JOIN dept_location dl ON (e.dno = dl.dnumber) LEFT JOIN (dl.dnumber = p.dnum AND dl.location = "Houston") LEFT JOIN work_on w ON (p.pnumber = w.pno) LEFT JOIN depedent dep ON (w.essn = dep.essn) SORT BY lname
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.