Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ANS2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

QA

1)
sql

SELECT STUDENT_NAME
FROM STUDENT
WHERE GENDER = 'male' AND BIRTHDATE > '2000-10-01';

(2)
sql

SELECT STUDENT_NAME
FROM STUDENT
WHERE LENGTH(ADDRESS) >= 8
ORDER BY STUDENT_NAME ASC;

(3)
sql

SELECT COURSE_TITLE
FROM COURSE
WHERE STAFF_ID IN ('BEAN', 'SAM');

(4)
vbnet

SELECT STAFF_ID, COUNT(COURSE_ID) AS NUM_COURSES


FROM COURSE
GROUP BY STAFF_ID
ORDER BY STAFF_ID ASC;

(5)
sql

SELECT S1.STAFF_NAME
FROM STAFF S1, STAFF S2
WHERE S1.BOSS_ID = S2.STAFF_ID
AND S1.SALARY > S2.SALARY;

(6)
sql

SELECT STAFF_NAME
FROM STAFF
WHERE SALARY < (SELECT AVG(SALARY) FROM STAFF);
(7)
Sql

SELECT COURSE_TITLE
FROM COURSE
WHERE COURSE_ID NOT IN (SELECT COURSE_ID FROM TAKEN);

(8)
sql

SELECT S1.STAFF_NAME
FROM STAFF S1, STAFF S2
WHERE S1.STAFF_ID = S2.BOSS_ID
AND S2.DEPARTMENT = 'Engineering'
AND S1.GENDER = 'male';

(9)
Sql

SELECT T1.STU_ID, T1.STUDENT_NAME


FROM STUDENT T1, COURSE T2
WHERE NOT EXISTS (
SELECT COURSE_ID
FROM COURSE
WHERE STAFF_ID = '007'
AND COURSE_ID NOT IN (
SELECT COURSE_ID
FROM TAKEN
WHERE STU_ID = T1.STU_ID
)
);

QB

Explanation:
(1) To retrieve the birthdays of managers:
SELECT Bdate FROM EMPLOYEE WHERE Ssn IN (SELECT Mgr_ssn FROM DEPARTMENT)
This query will select the birthdate ('Bdate') from the 'EMPLOYEE' table where the 'Ssn' matches
any 'Mgr_ssn' from the 'DEPARTMENT' table.
To retrieve the birthdays of managers, we need to join the EMPLOYEE and
DEPARTMENT tables on the Mgr_ssn and Ssn attributes respectively. Then,we can filter the result
to only show the birthdays of the managers.
Query:
1 SELECT E.Bdate
2 FROM EMPLOYEE E, DEPARTMENT D
3 WHERE E.Ssn=D.Mgr_ssn;
4
Output:
1 1962-09-15
2 1955-12-08
3 1969-03-29
4
Therefore,the birthdays of managers are September 15,1962;December 8,1955;and March
29,1969.
Note:The output may differ depending on the date format of the DBMS.
(2) To retrieve the salaries of all male employees working in the department No.5: SELECT 'Salary'
FROM 'EMPLOYEE' WHERE 'Sex' = 'M' AND 'Dno'='5'
This query will select the 'Salary' from the 'EMPLOYEE' table where Sex is 'M' and Dno is 5.
Note: This assumes that the 'Dno' column in the 'EMPLOYEE' table corresponds to the 'Dnumber'
column in the 'DEPARTMENT' table.

Explanation:
(3) To list the last names of employees in the Research department whose salary is above 35000:
SELECT Lname FROM EMPLOYEE WHERE Dno =5 AND Salary>35000
This query will select the 'Lname' from the 'EMPLOYEE' table where 'Dno' is '5' and 'Salary' is
greater than '35000'.

(4) To retrieve the last names and Ssns of employees who work on all projects:

SELECT Lname, Essn


FROM EMPLOYEE
WHERE NOT EXISTS (
SELECT Pnumber
FROM PROJECT
WHERE NOT EXISTS (
SELECT Essn
FROM WORKS_ON
WHERE PROJECT.Pnumber = WORKS_ON.Pnumber
AND EMPLOYEE.Ssn = WORKS_ON.Essn
)
)
This query will select the 'Lname' and 'Essn' from the 'EMPLOYEE' table where there does not
exista Pnumber in the 'PROJECT' table for which there does not exist an 'Essn' in the 'WORKS_ON'
table such that the 'Pnumber' and 'Essn' match the corresponding columns in the 'PROJECT' and
'EMPLOYEE' tables.
(5) To find the last names of male employees who are not supervised by 'Franklin Wong:

SELECT Lname
FROM EMPLOYEE
WHERE Sex = 'M'
AND Super_ssn <> (
SELECT Ssn
FROM EMPLOYEE
WHERE Lname = 'Wong'
AND Fname = 'Franklin'
)
This query will select the 'Lname' from the 'EMPLOYEE' table where 'Sex' is 'M' and 'Super_ssn'
does not match the 'Ssn' of an employee whose 'Lname' is 'Wong' and whose 'Fname' is
'Franklin'.

(6) To list the first names of all employees who work on the project 'Productx' and have a
daughter as the dependent:

SELECT Fname
FROM EMPLOYEE
WHERE Essn IN (
SELECT Essn
FROM DEPENDENT
WHERE Sex = 'F'
AND Relationship = 'Daughter'
AND Esse IN (
SELECT Essn
FROM WORKS_ON
WHERE Pno IN (
SELECT Pnumber
FROM PROJECT
WHERE Pname = 'ProductX'
)
)
)
This query will select the 'Fname' from the 'EMPLOYEE' table where the 'Essn'
matches an Esse in the DEPENDENT table where the Sex is 'F', the 'Relationship' is 'Daughter', and
the 'Esse' matches an 'Essn' in the 'WORKS_ON' table where the 'Pno' matches a 'Pnumber' in the
'PROJECT' table where the 'Pname' is 'ProductX'.

(7)The last name of each department manager who has at least one daughter as a dependent:

SELECT DISTINCT e.Lname


FROM EMPLOYEE e, DEPARTMENT d, DEPENDENT dep
WHERE e.Ssn = d.Mgr_ssn
AND dep.Essn = e.Ssn
AND dep.Sex = 'F'
AND dep.Relationship = 'Daughter';

Lname
Wallace

(8) Retrieving the names of employees who have no dependents are:

SELECT DISTINCT e.Lname, e.Fname


FROM EMPLOYEE e
WHERE NOT EXISTS (
SELECT d.Essn FROM DEPENDENT d
WHERE d.Essn = e.Ssn
);

Lname
Zelaya
Narayan
English
Jabbar
Borg

Fname
Franco
Alicia
Ranesh
Joyce
Ahmad
James

(9) The last names and Ssns of employees who work on at least one project in Houston and
whose department is located in Sugarland or Bellaire:

SELECT DISTINCT e.Lname, e.Ssn


FROM EMPLOYEE e, WORKS_ON w, PROJECT p, DEPT_LOCATIONS d
WHERE e.Ssn = w.Essn
AND w.Onumber = p.Pnumber
AND p.Plocation = 'Houston'
AND e.Dno = d.Dnumber
AND d.Dlocation IN ('Sugarland', 'Bellaire');
Lname
Wallace
English
Smith

Ssn
453453453
123456789
987654321

You might also like