[Solved] MySQL query to output free seats

EverSQL Database Performance Knowledge Base

MySQL query to output free seats

Database type:

I am working on a theatre booking system in MySql and I have three tables:

Production (contains Title, BasicTicketPrice), 
Performance (PerformanceDate, PerformanceTime, Title) 
Booking (Email of person who booked, PerformanceDate, PerformanceTime, RowNumber).

I need to write a query which will display only one column: RowNumber from Booking but it needs to display ONLY the seats which are available on the PerformanceDate 2017-11-01 at PerformanceTime 19:00:00.

So far I tried:

SELECT RowNumber
FROM booking
WHERE Email IS NULL

Because each user can book a ticket using their email but the email is constrained by NOT NULL condition so I guess that is why my query doesn't work.

Any ideas on what I should try to make this work? I will be very grateful for any suggestions. Thanks a lot!

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `booking` ADD INDEX `booking_idx_email` (`Email`);
The optimized query:
SELECT
        booking.RowNumber 
    FROM
        booking 
    WHERE
        booking.Email IS NULL

Related Articles



* original question posted on StackOverflow here.