Business Studies, asked by mubin7, 9 months ago

write a SQL query to display user id and number of tickets booked by each user.give an alias name as no_of_tickets.sort the result based on number of tickets booked.​

Answers

Answered by poojan
11

Query:

SELECT USER_ID, COUNT(TICKET_ID) AS NO_OF_TICKETS

FROM TICKETS

GROUP BY USER_ID

ORDER BY NO_OF_TICKETS;

Note:

The query's related relations are given as an attachment below through which we get to know what to extract and from which table.

Explanation:

As given in the question, we need to extract user_id and no of tickets booked by each user (note that the no of tickets one has booked can be estimated only by the ticket_ids),

To retrieve those values we select user_id and use count() to get the no of tickets by ticket id that user has booked - as count(ticket_id)

As it is given that the alias of that count should be given as no_of_tickets, the phrase as no_of_tickets is used.

So, we get select user_id, count(ticket_id) as no_of_tickets for now, and these are extracted from the table name tickets

Then to get the no of tickets each user has booked, we need to group the ticket_id by their user_id, Such that all the tickets that are issued with single id are grouped.

group by user_id

and the sorting is done by using order by phrase in ascending order as per the count of tickets from the least count booked user to the highest one. Here we use alias name; i.e.,

order by no_of_tickets;

Therefore, the complete query will be

SELECT USER_ID, COUNT(TICKET_ID) AS NO_OF_TICKETS

FROM TICKETS

GROUP BY USER_ID

ORDER BY NO_OF_TICKETS;

Learn more:

1. What are the maximum and the minimum number of rows returned by the R1 right outer join R2?

brainly.in/question/21195376

2. Write SQL commands for (a) to (e) on the basis of FAMILY relation given below:

brainly.in/question/15115256

Attachments:
Similar questions