Task: In this task you are expected to write an SQL query that will return the data asked for in the question. An SQL query works by selecting data from a table where certain conditions apply. A table contains columns where every row in that table must have a value for each column. Every table has a primary key that uniquely identifies each row, usually an id. To choose which columns are returned you specify that after the "SELECT" statement. Next, you use a "FROM" statement to specify what tables you want to select the data from. When you specify a table you can rename it with the "AS" statement. You can reference that table by whatever name follows the "AS" statement. If you want to select data from multiple tables you need to use the "JOIN" statement. This will join the tables together by pairing a row in one table with every row in the other table (Cartesian Product). To limit the number of rows returned you should use the "ON" statement. This will only return rows where the condition specified after the statement is true, this is usually an equals operator with primary keys. You can also use the "WHERE" statement to specify that only rows with column values statisfying a certain condition, should be returned. The "GROUP BY" statement will group rows together that have equal column values for whatever columns follows the statement. The "HAVING" statement will return groups that statisfy whatever condition follows the statement. Any column(s) being returned from grouped rows must either be an aggregate function, (AVG, MAX, COUNT, SUM, ...) of a column, or the column(s) that the data was grouped by. To sort the returned data you can use the "ORDER BY" command which will order the data by whatever aggregate function or column follows the statement. The "DESC" statement will sort in descending order and the "ASC" statement will sort in ascending order. Finally, you can use the "LIMIT" statement to return a certain number of rows. When "*" is used in an SQL statement every column is returned. For example, SELECT * FROM table WHERE attribute = 1, will select every column from rows with the attribute column equal to 1.
Categories: Text to Code
Domains: Code -> Language -> SQL

Positive examples:
input: Find the id and city of the student address with the highest average monthly rental.
output: SELECT T2.address_id ,  T1.city FROM Addresses AS T1 JOIN Student_Addresses AS T2 ON T1.address_id  =  T2.address_id GROUP BY T2.address_id ORDER BY AVG(monthly_rental) DESC LIMIT 1
explanation: First we select the student's id and city of their address. Next, to find where each student lived we must join the "Addresses" table with the "Student_Addresses" table on rows with the same "address_id". Finally, we want to return the student address with the highest monthly rent. This is a good example.; input: Show the facility codes of apartments with more than 4 bedrooms.
output: SELECT T1.facility_code FROM Apartment_Facilities AS T1 JOIN Apartments AS T2 ON T1.apt_id  =  T2.apt_id WHERE T2.bedroom_count  >  4
explanation: First we select the apartment's facility code. Next, the facility codes are stored in the "Apartment_Facilities" table, so we need to join that table with the "Apartments" table on rows with the same "apt_id". Finally we only return rows where the "bedroom_count" higher than 4. This is a good example.; input: Show invoice dates and order id and details for all invoices.
output: SELECT T1.invoice_date ,  T1.order_id ,  T2.order_details FROM Invoices AS T1 JOIN Orders AS T2 ON T1.order_id  =  T2.order_id
explanation: First we select the "invoice_date", "order_id", and "order_details". Next because we want the order details for each invoice we must join the "Invoices" and "Orders" table. Finally, we select rows where the "order_id" are the same. This is a good example.

Negative examples:
input: Show the average room count of the apartments that have booking status code "Provisional".
output: SELECT Avg ( T1.bathroom_count ) FROM Apartments AS T1 JOIN Apartment_Bookings AS T2 ON T1.apt_id = T2.apt_id WHERE T2.booking_status_code = "Provisional"
explanation: The average "bathroom_count" is returned instead of the average "room_count". This is a bad example.; input: Show all product names and the total quantity ordered for each product name.
output: SELECT product_name , Sum ( * ) FROM Products GROUP BY product_name
explanation: The sum of every column is returned instead of returning the sum of the "quantity_ordered" column for each product. The question asked for the sum of quantities ordered. This is a bad example.; input: What are the titles and directors of all movies that have a rating higher than the average James Cameron film rating?
output: SELECT T1.title , T1.director FROM Movie AS T1 JOIN Rating AS T2 ON T1.mID = T2.mID WHERE T2.stars > ( SELECT Avg ( T2.stars ) FROM Rating AS T2 )
explanation: In the WHERE statement the average for all movies is calculated. The question asked for movies with a higher rating than the average "James Cameron" movie. This is a bad example.