ANSI SQL - Commonly Made Mistakes Ebox
ANSI SQL - Commonly Made Mistakes Ebox
ANSI SQL - Commonly Made Mistakes Ebox
The list of mistakes that the trainees are doing during their ANSI SQL Ebox Certification which
has been prepared based on their reports from Ebox.
8/14/2015
0
Commonly made mistakes:
ALIAS Names
Columns selection
Others
o Clear understanding of the given Database schema and its relationship representations
1
even before attempting the questions
o Analyze the tables and understand the case study table structure before writing queries.
For Ex: There might be 10 books in a library, but it is not necessary that all the books
have been taken at least once. Hence join should not be used between these two tables.
o When there are non-aggregate columns in the SELECT clause, those non-aggregated
columns should be included in GROUP BY clause.
SELECT abc, efg, count (abc) from xyz table group by abc
[Note: The above query is only supported in MySQL and not a standard way of grouping.]
SELECT abc, efg, count (abc) from xyz table group by abc, efg
If you use any function for a column for which the ALIAS name is not specified in the question,
then provide the column name as the ALIAS name.
o Ex: Question: select the contact and the name of employees. Print ‘N/A’ if contact is not
available.(if mentioned in the question)
Select IFNULL (B.CONTACT,'N/A'), emp_name from employee
o In the above case, ALIAS is not specified in the question, but a function is being used in
the query. Hence use the column name as ALIAS
o Correct Query is
Select IFNULL (B.CONTACT,'N/A') CONTACT, emp_name from employee
Other
2
Commonly made mistakes - Examples:
1. Added extra columns in the select clause which is not asked in the problem statement. For
example,
a. Problem statement is Write a query to display issue id, customer id, customer name
for the customers but the trainee’s query is select
b.issue_id,b.customer_id,c.customer_name,b.return_date,b.actual_date_of_return
2. Hardcoding – For example,
a. Problem is to find the customers who has taken movie more than once, but the trainee
has hardcoded the values of the customers who has taken movie more than once, like
this c.customer_id='C00004', But this is work fine in the default data but will fail in the
actual data which we use to validate the answers.
b. One more example, case c.contact_no when '9830354218' then '+91-983-035-4218'
when '8923156781' then '+91-892-315-6781'
when '9831289761' then '+91-983-128-9761' end as CONTACT_ISD,
3. Solution Query Failed to Execute Reason: MySQLSyntaxErrorException. For example,
a. select m.customer_name,coalesce(m.contact_no,m.contact_address) as
CONTACT_DETAILS,if(lm.card_id) is null from library_card_master lm,
lm.description from library_card_master lm
How will control statement work in the select clause?
b. if mm.movieid is NULL then '0'; - Trainee has put this condition after join without
“where” clause.
c. Select substring(id.issue_id ___%d) as SERIAL_NO – How will this work?
4. Incomplete Queries - select customer_id,customer_name,age,concat('+91','-',contact_no) from
customer_master – Which is not as per requirements.
5. Missed condition – For example,
a. Display the customers who has contact number, but many trainees missed this
condition in the query.
b. Display movies which are not issued to any customers, this condition is missed by many
trainees.
6. Query which is logically wrong.
7. Typo in the query
8. Solution Query Failed to Execute Reason: SQLException: Subquery returns more than 1 row. For
example,
a. Where director_name = ( select director_name from movies_master group by
director_name) – Always use “IN” clause. Default test data would have returned single
row, but actual data returned more than one row which resulted in failure. Many
trainees have made these mistakes.
9. Solution Query Failed to Execute Reason: MySQLSyntaxErrorException: Unknown column
'customer_id.cid' in 'field list'. For example,
a. Select customer_id.cid,sum(rental_cost) as TOTAL_COST from customer_issue_details
3
cid join – How will this work? Putting alias name after column name.
10. Duplicate rows – Missed primary key column matching in the query.
11. Wrong syntaxes – for example,
a. having coalesce(contact,address) – How will this work?