TASK DEFINITION: In this task you are expected to fix an SQL query based on feedback. You will be given an SQL statement and an English description with what is wrong about that SQL statement. You must correct the SQL statement based off of the feedback. 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. 
PROBLEM: SQL: SELECT Product_Name FROM Products GROUP BY Product_Name HAVING Count ( * ) > = 2
Feedback: In step 1 use product id instead of product name and products in events instead of products , in step 2 remember to list names in alphabetical order .

SOLUTION: SELECT T1.Product_Name FROM Products AS T1 JOIN Products_in_Events AS T2 ON T1.Product_ID  =  T2.Product_ID GROUP BY T1.Product_Name HAVING COUNT(*)  >=  2 ORDER BY T1.Product_Name

PROBLEM: SQL: SELECT Document_Type_Code FROM Documents GROUP BY Document_Type_Code ORDER BY Count ( * ) Desc LIMIT 1
Feedback: Replace document type code in documents table with budget type code in documents with expenses for all .

SOLUTION: SELECT budget_type_code FROM Documents_with_expenses GROUP BY budget_type_code ORDER BY count(*) DESC LIMIT 1

PROBLEM: SQL: SELECT T1.Owner FROM program AS T1 JOIN broadcast AS T2 ON T1.Program_ID = T2.Program_ID WHERE T2.Time_of_day = "Morning" INTERSECT SELECT T3.Owner FROM channel AS T3 JOIN broadcast AS T2 ON T3.Channel_ID = T2.Channel_ID WHERE T2.Time_of_day = "Night"
Feedback: Replace channel with program in step 4.

SOLUTION:
SELECT t1.owner FROM program AS t1 JOIN broadcast AS t2 ON t1.program_id  =  t2.program_id WHERE t2.Time_of_day  =  "Morning" INTERSECT SELECT t1.owner FROM program AS t1 JOIN broadcast AS t2 ON t1.program_id  =  t2.program_id WHERE t2.Time_of_day  =  "Night"