Question

Generate a list of employees (last name and first name) and their supervisors (last name and...

Generate a list of employees (last name and first name) and their supervisors (last name and first name of supervisor aliased to ReportsToLastName and ReportsToFirstName).

(Hint: When joining a table to itself, you must use table aliases.)

-- Show your answers both in self-join as well as recursive CTE. CREATE a self-join and recursive CTE for the above query.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

--Creating an example employee table :

CREATE TABLE employees

(

  employee_id int NOT NULL PRIMARY KEY,

  first_name varchar(50) NOT NULL,

  last_name varchar(50) NOT NULL,

  supervisor_id int NULL

);

--Insert some random example data

INSERT INTO employees VALUES (1, 'JJ', 'thyns', NULL);

INSERT INTO employees VALUES (2, 'Tem', 'Rob', 1);

INSERT INTO employees VALUES (3, 'Dino', 'Durel', 1);

INSERT INTO employees VALUES (4, 'Rob', 'Peter', 2);

INSERT INTO employees VALUES (5, 'Kent', 'Fryn', 2);

INSERT INTO employees VALUES (6, 'Bill', 'Gates', 3);

INSERT INTO employees VALUES (7, 'John', 'Miller', 3);

INSERT INTO employees VALUES (8, 'Shyan', 'Markester', 5);

INSERT INTO employees VALUES (9, 'Jacob', 'Matthew', 6);

INSERT INTO employees VALUES (10, 'Michael', 'Jackson', 6) ;

Query using ‘self join’ (Without adding the top employee who has no supervisor) :

SELECT e.last_name as LastName, e.first_name as FirstName,

               s.last_name as ReportsToLastName, s.first_name as ReportsToFirstName

FROM employees e

JOIN employees s ON s .employee_id = e.supervisor_id ;

Query using ‘self join’ (With the topmost supervisor ) :

SELECT e.last_name as LastName, e.first_name as FirstName,

               s.last_name as ReportsToLastName, s.first_name as ReportsToFirstName

FROM employees e

LEFT JOIN employees s ON s .employee_id = e.supervisor_id ;



Query using ‘With’ clause to show recursive CTE : (Note: This query is written in PostgreSQL, if using SQL server replace “WITH RECURSIVE” with “WITH” )

WITH RECURSIVE cte (emp_id,fname, lname, supervisor_id, emplevel)

AS

  (

    SELECT employee_id, first_name, last_name, supervisor_id, 1

    FROM employees

    WHERE supervisor_id IS NULL

    UNION ALL

    SELECT e.employee_id, e.first_name, e.last_name, e.supervisor_id,

      r.emplevel + 1

    FROM employees e

      INNER JOIN cte r

        ON e.supervisor_id = r.emp_id

  )

SELECT lname as LastName, fname as FirstName,

  (SELECT last_name

   FROM employees

    WHERE employee_id = cte.supervisor_id) AS ReportsToLastName ,

(SELECT first_name

   FROM employees

    WHERE employee_id = cte.supervisor_id) AS ReportsToFirstName

FROM cte ;

Output screenshots:

Add a comment
Know the answer?
Add Answer to:
Generate a list of employees (last name and first name) and their supervisors (last name and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need this written in SQL Employee EMP ID EMP FNAME EMP LNAME EMP STREET EMP...

    I need this written in SQL Employee EMP ID EMP FNAME EMP LNAME EMP STREET EMP CITY EMP STATE EMP ZIP EMP START DATE Table TABLE ID AREA IDTABLE SEATS Area AREA ID AREA NAME AREA SUPERVISOR EMPLOYEE ID Customer CUST ID CUST LAST NAME CUST NUMBER OF GUESTS Assignment EMPID TABLE ID Seating CUST IDTABLE ID SEATING COST SEATING DATE SEATING TIP Question 29 (10 points) Write an SQL query to list the employee ID and first and last...

  • 1. Write a query to list the first name and last name of those pilots who...

    1. Write a query to list the first name and last name of those pilots who were hired after January 1, 2010. Sort your result by the ascending order of last name. Within matching last names, order by first name. 2. Write a query to list flight number, pilot number, pilot last name for all flights whose destinations are using the IN keyword. 3. Write a query to list the names and telephone numbers of the passengers who have reservations...

  • can someone solve these quick please and thank you! sql chapter 4 Query #1: List the...

    can someone solve these quick please and thank you! sql chapter 4 Query #1: List the company name, contact name, contact title and the phone number for customers who HAVE NOT put in an order. Use an outer join. Query #2: Create a listing displaying the employee first name, last name and the full name (First name space Last Name) of the person they report to (Supervisor). This is a self-join. If an employee does not report to anyone then...

  • SQL queries and procedures TABLE: Employees Business Rules: EmployeeID is defined as the primary key. Address...

    SQL queries and procedures TABLE: Employees Business Rules: EmployeeID is defined as the primary key. Address has been denormalized to include City and State for performance reasons. DeptNbr is a foreign key that references DeptNbr in the Department table Manager is a foreign key that references EmployeeID in the Employees table Data Structure: (EmployeeId, LastName, FirstName, Street, City, State, Zip, DateOfHire, DateOfBirth, JobTitle, Salary, DeptNbr(fk), Manager(fk)) TABLE: Department Business Rules: DeptNbr is defined as the primary key. Data Structure: (DeptNbr,...

  • Write an sql query to list the customers last name, seating cost, seating tip and service...

    Write an sql query to list the customers last name, seating cost, seating tip and service date for all customers with last name Smith or jones or brown. SAMPLE DATA BASE (Key fields are underlined.) Employee EMP_ID EMP_FNAME EMP_LNAME EMP_STREET EMP_CITY EMP_STATE EMP_ZIP EMP_START_DATE Table TABLE D AREA ID TABLE SEATS Area AREA_ID AREA NAME AREA SUPERVISOR EMPLOYEE_ID Customer CUST ID CUST_LAST_NAME CUST_NUMBER_OF_GUESTS Assignment EMP ID TABLE ID Seating CUST_ID TABLE_ID SEATING COST SEATING DATE SEATING TIP

  • Using mySQL, tables at the end... This assignment uses the tables from the vets database. The goa...

    Using mySQL, tables at the end... This assignment uses the tables from the vets database. The goal of the assignment is to get you to think about joins- inner join and outer joins.  If you do a join, where you have the names of two or more tables in the From clause, then you must use the condition join syntax or the column name join. Take care that you do not accidentally do a Cartesian product. If your result set...

  • Which SQL statement retrieves the EmployeeName and City columns from the Employees table? O LIST EmployeeName,...

    Which SQL statement retrieves the EmployeeName and City columns from the Employees table? O LIST EmployeeName, City ON Employees; O FIND EmployeeName, City ON Employees; SELECT EmployeeName, City FROM Employees; O RETRIEVE EmployeeName, City FROM Employees; Which SQL statement finds the different cities in which national parks reside with no duplicate rows listed? SELECT DISTINCT city FROM natlpark ORDER BY city; O ORDER BY natlpark DISTINCT city; O ORDER BY city FROM natlpark; O SELECT city FROM natlpark ORDER BY...

  • (TCO 6) Write a query to list the customer first name, last name as a single...

    (TCO 6) Write a query to list the customer first name, last name as a single field with a heading of Customer along with the balance sorted by balance from lowest to highest. SalesRep Customer PK ReplD PK varchar(20) varchar(20) decimal(10,2) CustomerID int -OH Last Name FirstName Last Name varchar(20) Commission Rate FirstName varchar(20) Street varchar(20) City varchar(20) Order State char(2) Zipcode char(5) HO. ---OPK OrderID Balance decimal(10,2) ReplD int FK1 CustomeriD OrderDate ShipDate Part int int date date PK...

  • 1,List the first and last name of the donators that donated in December 2009. 2. List...

    1,List the first and last name of the donators that donated in December 2009. 2. List the ID's of the volunteers that have not worked. 3. List the ID, first name, and last name of any employees who works in the Finance department. 4. List all the different prices suppliers have for 'Tasty Meat' product. 5. Select the store ID and store phone number for all stores in 'St.Paul'. 6. List the member first and last name, address, city, zip...

  • Precision Manufacturing produces machine parts and has nearly 200 production employees and 50 employees in its...

    Precision Manufacturing produces machine parts and has nearly 200 production employees and 50 employees in its front office with responsibilities ranging from data entry to marketing. Jackson Smith is the new compensation manager at Precision, and his first task is to implement a merit pay program that would tie to the company’s performance appraisal process. For the last 10 years, all employees have received an annual pay increase, but it has been an across-the-board increase, with all employees receiving the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT