We may need following information to form an SQL statement
1. Table name (One or more)
2. Attribute names (includes aggregate function as attribute) of each table
3. Keywords
4. Conditions
5. Aggregate functions
A SELECT type SQL can have the following KEYWORD components. They must be written IN ORDER.
SELECT <attribute(s)>
FROM <table(s)>
WHERE <condition(s)>
GROUP BY <Attribute>
HAVING <aggregate condition(s)>
ORDER BY <attribute(s)>
Example of out of order KEYWORDS; Hence WRONG.
SELECT TeamName, BarBill FROM Member ORDER BY TeamName WHERE BarBill <100;
Example of misplaced attributes; Hence WRONG.
SELECT Avg(BarBill) FROM Member Avg(BarBill), MemberType ORDER BY Avg(BarBill), MemberType DESC;
Example of misplaced ON and missing table name; Hence WRONG.
SELECT * FROM Member INNER JOIN Tournament.TourName ON Member.MemberID ORDER BY MemberID;
–Q6. List the details for all members whose type is either Junior or Senior and who also have a handicap less than 25
Example of WRONG combining multiple conditions.
Q. List the details for all members whose type is either Junior or Senior and who also have a handicap less than 25.
SELECT * from Member WHERE MemberType = ‘Junior’ or MemberType = ‘Senior’ and Handicap < 25;
Example of WRONG use of ORDER BY.
Q. List the details for all members. Sequence the output by name within gender
SELECT * from Member ORDER BY MemberName, Gender;
Example of WRONG use of WHERE.
Q. List the member id and entry count for each member who has entered more than 2 tournaments.
SELECT MemberId, count(*) “Entry Count” from TourEntry WHERE count(*) > 2;