skip to Main Content

Write a query to display the courier details such as courier id with the alias name ‘COURIER1’ and the courier id of the courier that got delivered on the same date as courier1 with the alias name ‘COURIER2’ and the delivered date.

Sort the records by the delivery date in descending order and then by the courier id of courier1 in descending order.

Display the output in RDBMS.

2

Answers


  1. SELECT 
        c1.courier_id AS COURIER1,
        c2.courier_id AS COURIER2,
        c2.delivered_date
    FROM 
        couriers c1
    JOIN 
        couriers c2 ON c1.delivered_date = c2.delivered_date AND c1.courier_id <> c2.courier_id
    WHERE 
        c1.courier_id = 'your_courier_id'
    ORDER BY 
        c2.delivered_date DESC, c1.courier_id DESC;
    
    Login or Signup to reply.
  2. Here is the solution:

    SELECT 
        c1.courier_id AS COURIER1,
        c2.courier_id AS COURIER2,
        c2.delivered_date
    FROM 
        courier_details c1
    JOIN 
        courier_details c2 ON c1.delivered_date = c2.delivered_date
    WHERE 
        c1.courier_id = 'COURIER1'
        AND c2.courier_id != 'COURIER1'
    ORDER BY 
        c2.delivered_date DESC,
        c1.courier_id DESC;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search