Question

In: Computer Science

SQL ONLY. WRITE CLEAR AND SIMPLE ANSWERS. Consider the following relations (PRIMARY KEYS ARE WRITTEN IN...

SQL ONLY. WRITE CLEAR AND SIMPLE ANSWERS.

Consider the following relations (PRIMARY KEYS ARE WRITTEN IN BOLD) departments (dept_no, dept_name) dept_emp (emp_no, dept_no, from_date, to_date) dept_manager (dept_no, emp_no, from_date, to_date) employees (emp_no, birth_date, first_name, last_name, gender, hire_date) salaries (emp_no, salary, from_date, to_date) titles(emp_no, title, from_date, to_date) Write the following queries in SQL. No duplicates should be printed in any of the answers. List all the titles for which there is at least one employee having the title. Find the current employee(s) (only id) who has/have the most (highest) experience working in a department [across all departments]. Here, we are referring to working only in one department. For example, let's assume there are only two employees: A and B. Now, A has been working in department X for 15 years and B worked in department X for 10 years and is working in department Y for 8 years. Although B has worked for a total of 18 years, he worked for 10 years in one department. So, for this query the output should be the id of A because A has worked for 15 years in one department which is the highest. Find the names (both first and last) and current salary of all employees who were hired after 31st December 1998. (Hint: The to_date for current salary is '9999-01-01') Find the id and name (both first and last) of all employees who are currently not in the 'Development' department and whose first name start with 'Tom'. Find the names (both first and last) and current salaries of all employees who earn more than the current average salary of all employees. List them in the descending order based on their salary (highest salary first). Some employees have worked in multiple departments. Find the names (both first and last) and the number of departments of all the female employees who have worked in more than one department. Write SQL query to find out the name and current average salary of the department that has the maximum current average salary? Find the employee id of the current managers who currently manages more than 16000 employees. Find the name and current average salary of the departments whose current average salary is more than the current average salary of 'Development' department. Find the name of the employees who currently have the same salary but currently works in different department.

SQL ONLY. WRITE CLEAR AND SIMPLE ANSWERS. READ THE QUESTIONS CAREFULLY. IF AN ANSWER IS WRONG, I WILL DOWNVOTE.

Solutions

Expert Solution

SQL QUERY FOR ABOVE QUESTIONS:

(1) List all the titles for which there is at least one employee having the title.

SELECT TITLE FROM TITLES HAVING COUNT(EMP_NO)>=1

(2) Find the current employee(s) (only id) who has/have the most (highest) experience working in a department [across all departments].
Here, we are referring to working only in one department. For example, let's assume there are only two employees: A and B.
Now, A has been working in department X for 15 years and B worked in department X for 10 years and is working in department Y for 8 years.
Although B has worked for a total of 18 years, he worked for 10 years in one department.
So, for this query the output should be the id of A because A has worked for 15 years in one department which is the highest.

SELECT EMP_NO, EXTRACT(YEAR FROM AGE(CURRENT_DATE, HIRE_DATE)) AS YEAR_OF_EXPERIENCE FROM EMPLOYEES ORDER BY YEAR_OF_EXPERIENCE DESC LIMIT 1

(3) Find the names (both first and last) and current salary of all employees who were hired after 31st December 1998. (Hint: The to_date for current salary is '9999-01-01')

SELECT FIRST_NAME, LAST_NAME, SALARY
FROM EMPLOYEES JOIN SALARIES ON EMPLOYEES.EMP_NO=SALARIES.EMP_NO
WHERE HIRE_DATE > CONVERT(DATETIME, '1998-12-31') AND TO_DATE='9999-01-01'

(4) Find the id and name (both first and last) of all employees who are currently not in the 'Development' department and whose first name start with 'Tom'.

SELECT E.EMP_NO,E.FIRST_NAME, E.LAST_NAME FROM EMPLOYEES E
JOIN DEPT_EMP DE ON E.EMP_NO = DE.EMP_NO
JOIN DEPARTMENTS D ON D.DEPT_NO=DE.DEPT_NO
WHERE DE.DEPT_NAME !='Development' AND E.FIRST_NAME LIKE 'Tom%'

(5) Find the names (both first and last) and current salaries of all employees who earn more than the current average salary of all employees.
List them in the descending order based on their salary (highest salary first).

SELECT E.FIRST_NAME, E.LAST_NAME,SALARY
FROM EMPLOYEES Join SALARIES S ON S.EMP_NO = E.EMP_NO
WHERE SALARY > (SELECT AVG (SALARY) FROM SALARIES) AND TO_DATE='9999-01-01' ORDER BY SALARY DESC

(6) Some employees have worked in multiple departments.
Find the names (both first and last) and the number of departments of all the female employees who have worked in more than one department.

SELECT E.FIRST_NAME, E.LAST_NAME,count(dpt_name) FROM employees
JOIN DEPT_EMP ON E.EMP_NO=DEPT_EMP.EMP_NO
JOIN DEPARTMENTS on DEPARTMENTS.DEPT_NO = DEPT_EMP.DEPT_NO
WHERE E.GENDER="FEMALE"
GROUP BY E.FIRST_NAME, E.LAST_NAME
HAVING COUNT(DEPT_NAME) > 1

(7) Write SQL query to find out the name and current average salary of the department that has the maximum current average salary?

SELECT D.DEPT_NAME, MAX(AVG(SALARY)) AS MAX_AVG_SALARY FROM DEPARTMENTS D
JOIN DEPT_EMP DE ON D.DEPT_NO=DE.DEPT_NO
JOIN SALARIES ON DE.EMP_NO=SALARIES.EMP_NO WHERE TO_DATE='9999-01-01' ORDER BY MAX_AVG_SALARY DESC LIMIT 1

(8) Find the employee id of the current managers who currently manages more than 16000 employees.

SELECT EMP_NO FROM DEPT_MANAGER WHERE DEPT_NO IN (SELECT DEPT_NO FROM DEPT_EMP HAVING COUNT(DEPT_NO)>16000 GROUP BY DEPT_NO) AND TO_DATE='9999-01-01'

(9) Find the name and current average salary of the departments whose current average salary is more than the current average salary of 'Development' department.

SELECT DEPT_NAME,AVG(SALARY) AS AVG_SALARY FROM SALARIES S
JOIN DEPT_EMP DE ON S.EMP_NO=DE.EMP_NO
JOIN DEPARTMENTS D ON D.DEPT_NO = DE.DEPT_NO
WHERE AVG_SALARY >(SELECT AVG(SALARY) FROM SALARIES S
JOIN DEPT_EMP DE ON S.EMP_NO=DE.EMP_NO
JOIN DEPARTMENTS D ON D.DEPT_NO = DE.DEPT_NO WHERE D.DEPT_NAME='DEVELOPMENT' WHERE S.TO_DATE='9999-01-01' GROUP BY DEPT_NAME) AND S.TO_DATE='9999-01-01' GROUP BY DEPT_NAME

(10) Find the name of the employees who currently have the same salary but currently works in different department.

SELECT E.FIRST_NAME FROM EMPLOYEES E1
JOIN SALARIES S1 ON E.EMP_NO = S.EMP_NO
JOIN SALARIES S2 ON(S1.SALARY=S2.SALARY) AND (S1.EMP_NO < > S2.EMP_NO)) WHERE S1.TO_DATE='9999-01-01'


Related Solutions

SQL ONLY. WRITE CLEAR AND CORRECT ANSWERS. Consider the following relations (PRIMARY KEYS ARE WRITTEN IN...
SQL ONLY. WRITE CLEAR AND CORRECT ANSWERS. Consider the following relations (PRIMARY KEYS ARE WRITTEN IN BOLD) departments (dept_no, dept_name) dept_emp (emp_no, dept_no, from_date, to_date) dept_manager (dept_no, emp_no, from_date, to_date) employees (emp_no, birth_date, first_name, last_name, gender, hire_date) salaries (emp_no, salary, from_date, to_date) titles(emp_no, title, from_date, to_date) Write the following queries in SQL. No duplicates should be printed in any of the answers. List all the titles for which there is at least one employee having the title. Find the current...
1. Consider the following relations. The primary keys are underlined. All attributes are of type string...
1. Consider the following relations. The primary keys are underlined. All attributes are of type string if not indicated otherwise.  Student(s_ID, s_name, s_degree: integer, advisorID, d_ID)  Lecture(l_ID, l_name,l_degree: integer, p_ID, d_ID)  Register(s_ID,l_ID,score: integer, Semester)  Professor(p_ID,p_name, d_ID)  Department(d_ID, d_name, address) a. Find the names of professors who have taught in every semester. b. List the names of lectures that the CISE department offers but that are taught by a professor whose department is not CISE. c....
Consider the following relational schema about a University (the primary keys are underlined and foreign keys...
Consider the following relational schema about a University (the primary keys are underlined and foreign keys are italic) STUDENT(StudentID, name, major, year, age) CLASS(ClassName, meetsAt, room, LecturerID) ENROLLED(StudentID, ClassName, mark) LECTURER(LecturerID, name, DepartmentID) DEPARTMENT(DepartmentID, name) Write the SQL statements for the following query: B1. Find the age of the oldest student. B2. Find the ID’s of lecturers whose name begins with “K” \ B3. Find the age of the youngest student who is enrolled in Mechatronics. B4. Find the age...
Normalize the following relations. Show possible candidate/primary keys and the functional dependencies. Explain the normal form...
Normalize the following relations. Show possible candidate/primary keys and the functional dependencies. Explain the normal form in which the relation is currently in (based on the sample data) and how do you break the relations to get 3rd Normal Form. Show the new relations obtained after normalization and underline the candidate/primary key in each new relation and italic the foreign key. Relation 1: COLLEGE PARKING TICKET (STID, LName, FName, PhoneNo, StateLic, LicNo, Ticket#, Date, Code, Fine) (Illustrated with sample data)...
. Please write the SQL statements required to create the following relations, including appropriate versions of...
. Please write the SQL statements required to create the following relations, including appropriate versions of all primary and foreign key integrity constraints. (10 points) Books (isbn:CHAR(10), title: CHAR(20), author: CHAR(80), qty_in_stock: INTEGER, price: REAL, year_published: INTEGER) Customers (cid:INTEGER, cname: CHAR(80), address: CHAR(200)) Orders (ordernum:INTEGER, cid:INTEGER, order_date: DATE, cardnum:CHAR(16)) Orderlists (ordernum:INTEGER, isbn:CHAR(10), qty:INTEGER, ship_date:DATE)
Write the following questions as queries in SQL. Use only the operators discussed in class (no...
Write the following questions as queries in SQL. Use only the operators discussed in class (no outer joins) Consider the following database schema: INGREDIENT(ingredient-id,name,price-ounce) RECIPE(recipe-id,name,country,time) USES(rid,iid,quantity) where INGREDIENT lists ingredient information (id, name, and the price per ounce); RECIPE lists recipe information (id, name, country of origin, and time it takes to cook it); and USES tells us which ingredients (and how much of each) a recipe uses. The primary key of each table is underlined; rid is a foreign...
Consider the following three tables, primary and foreign keys. Table Name        SalesPeople Attribute Name                  &nbsp
Consider the following three tables, primary and foreign keys. Table Name        SalesPeople Attribute Name                                Type                                      Key Type EmployeeNumber             Number                               Primary Key Name                                   Character JobTitle                                  Character            Address                                 Character PhoneNumber                     Character YearsInPosition                             Number Table Name        ProductDescription Attribute Name                                Type                                      Key Type                 ProductNumber                Number                               Primary Key                 ProductName                  Character                            ProductPrice                   Number Table Name        SalesOrder Attribute Name                                Type                                      Key Type                 SalesOrderNumber        Number                               Primary Key                 ProductNumber               Number                               Foreign Key                 EmployeeNumber           Number                               Foreign Key                 SalesOrderDate                Date Assume that you...
Looking for SHORT, CLEAR, and SIMPLE answers --Trace the steps of a long and indirect signaling...
Looking for SHORT, CLEAR, and SIMPLE answers --Trace the steps of a long and indirect signaling pathway from a cell-surface receptor to a change in gene expression in the nucleus. Compare this pathway with an example of a short and direct pathway from the cell-surface to the nucleus. --Describe the G protein pathway. The basic structure for G-protein is composed of 3 subunits: alpha, beta, and gamma, sometimes refer to as heterotrimeric G-proteins. --Identify how protein kinases regulate protein phosphorylation,...
I need the specific answers for these questions, and clear vision diagram thanks Consider the following...
I need the specific answers for these questions, and clear vision diagram thanks Consider the following tables: GENRE(GEN_CODE, GEN_DESCRIPTION) BOOK(BOOK_ISBN, BOOK_TITLE, BOOK_PRICE, GEN_CODE) For the tables given above, work on the problems below: 1. For each table, identify the primary key. 2. For each table, identify the foreign key(s). 3. Identify at least one candidate key. If there are no candidate keys, give an example of an attribute that could be used as a candidate key. 4. For one of...
Please implement this in Oracle sql 2.) Write a SELECT statement that answers this question: What...
Please implement this in Oracle sql 2.) Write a SELECT statement that answers this question: What is the total amount ordered for each product? Return these columns: The product name from the Products table The total amount for each product in the Order_Items (Hint: You can calculate the total amount by subtracting the discount amount from the item price and then multiplying it by the quantity) Use the ROLLUP operator to include a row that gives the grand total.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT