Question

In: Computer Science

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.

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

  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.

  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')

  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'.

  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).

  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.

  7. Write SQL query to find out the name and current average salary of the department that has the

    maximum current average salary?

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

    employees.

  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.

  10. Find the name of the employees who currently have the same salary but currently works in

    different department.

SQL ONLY. WRITE CLEAR AND CORRECT ANSWERS.

Solutions

Expert Solution

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_experince FROM employees order by year_of_experince 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).

Used the hint given in question 3 for current salary: (Hint: The to_date for current salary is '9999-01-01')

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(dpt_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 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...
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....
Please write clear grammatically correct answer. Answers that are not grammatically correct will be counted wrong....
Please write clear grammatically correct answer. Answers that are not grammatically correct will be counted wrong. Simplify your answers as much as possible. Do not write long paragraphs. Please answer all 4 questions. Name the four types of teeth from the front to the back of the dental arcade and describe the function of each type of tooth. Define the terms analogy and homology. Give an example of each and describe the significance of these terms in developing a classification...
Please write clear grammatically correct answer. Answers that are not grammatically correct will be counted wrong....
Please write clear grammatically correct answer. Answers that are not grammatically correct will be counted wrong. Simplify your answers as much as possible. Do not write long paragraphs. Answer all 4 questions. 14. Name the two most important environmental changes during the Miocene and describe their effects on primate evolution. a. b. 15. Name and describe the two phases of the walking cycle and the three main aspects of bipedal locomotion. 1. 2. a. b. c. 16. Compare ape-human differences...
Please write clear grammatically correct answer. Answers that are not grammatically correct will be counted wrong....
Please write clear grammatically correct answer. Answers that are not grammatically correct will be counted wrong. Simplify your answers as much as possible. Do not write long paragraphs. Answer all 3 questions. Name and justify your selection of the three most important differences between H. rudolfensis and H. habilis. Describe the significant physical differences between H. ergaster and H. erectus and why you think they should be classified as one or two species. What is the relationship between Neanderthals and...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT