18+ SQL best practices & optimisation interview Q&As _ 800+ Big Data & Java Interview FAQs
18+ SQL best practices & optimisation interview Q&As _ 800+ Big Data & Java Interview FAQs
Home › Quick Prep Big Data Interview › 300+ Big Data Interview FAQs 📌 › Big Data - 01: SQL 📌 › 00: 18+
SQL best practices & optimisation interview Q&As
SQL is very easy to learn, but lots of hands-on experience is required 100+ Java code quality Q&As
to master to perform the below tasks. 150+ Java coding Q&As
2) Writing efficient, readable & maintainable SQL. 300+ Big Data Interview
Q&As - Quick Prep FAQs
3) Breaking down & reverse engineering already functioning &
300+ Big Data Interview
complex SQL queries into business requirements to modify or
FAQs 📌
enhance.
250+ Scala Interview FAQs
#1. Use uppercase for the keywords like SELECT, FROM, JOIN, Key Areas & Companion
GROUP BY, WHERE, etc. It’s also a good practice to use uppercase Techs FAQs
for the SQL functions like UPPER(col_name), COUNT(o.id), etc.
16+ Tech Key Areas FAQs
Another key rule is that each clause such as SELECT, FROM, WHERE,
GROUP BY, HAVING etc. should be in a new line. Proper structure 10+ Companion Tech Q&As
improves readability of SQL queries.
https://www.java-success.com/15-sql-best-practices-interview-qas/ 1/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
1 select c.id, 800+ Enterprise & Core
2 c.name, Java Q&As
3 count(o.id) as orders_count
4 from my_schema.customers c
300+ Core Java Q&As
5 join my_schema.orders o ON c.id = o.customer_id
6 where c.age <= 30 300+ Enterprise Java Q&As
7 group by c.id, c.name
8
Tutorials - Golang
1 SELECT cust.id,
2 cust.name, Tutorials - Big Data
3 COUNT(ord.id) as orders_count
Tutorials - Enterprise Java
4 FROM my_schema.customers cust
5 JOIN my_schema.orders ord ON cust.id = ord.customer_id
6 WHERE cust.age <= 30
7 GROUP BY cust.id, cust.name
8
#2. Use column & table aliases where it makes sense. For example,
in the above example the column alias orders_count makes it more
readable as in “COUNT(ord.id) as orders_count”. Use table aliases
like cust, ord, etc shown above when you are joining multiple tables.
#3. Avoid select * as it hides the intentions behind your query. More
importantly, the table can grow later with new columns added, which
could intentionally impact the existing queries & scripts written with
“SELECT * FROM table…..”.
Avoid:
Avoid:
Favor:
1 SELECT COUNT(*)
2 FROM
3 (
4 SELECT c.name, c.age
5 FROM my_schema.customers c
6 GROUP BY c.name, c.age
7 )
8
https://www.java-success.com/15-sql-best-practices-interview-qas/ 3/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
Wrong:
Correct:
dt is a Derived Table.
Unnecessary JOINs are performed and when the data doubles the
DISTINCT keyword is used to incorrectly fix it. It is correct to do
SELECT … FROM employer WHERE EXISTS (SELECT… FROM
employee) as opposed to joining and selecting the DISTINCT rows.
Wrong:
1 SELECT *
2 , ROW_NUMBER() OVER(PARTITION BY name ORDER BY updated de
3 FROM user
4 WHERE row_num = 1
5
Correct:
https://www.java-success.com/15-sql-best-practices-interview-qas/ 4/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
1 SELECT *
2 FROM (
3 SELECT *
4 , ROW_NUMBER() OVER(PARTITION BY name ORDER BY updated de
5 FROM user
6 ) as k
7 WHERE k.row_num = 1
8
1
2 WITH cte_tbl AS
3 (
4 SELECT *, ROW_NUMBER() OVER(PARTITION BY name ORDER BY update
5 FROM user
6 )
7
8 SELECT FROM cte_tbl
9 WHERE row_num = 1
10
1
2 SELECT *
3 FROM user
4 QUALIFY ROW_NUMBER() OVER(PARTITION BY name ORDER BY updated d
5
https://www.java-success.com/15-sql-best-practices-interview-qas/ 5/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
#8. Write useful comments only when the logic is complex, but don’t
over do it. Use meaningful names & aliases to improve readability.
#9. Starting the WHERE clause with 1=1 allows you to comment
certain conditions for debugging purpose.
1 SELECT cust.id,
2 cust.name,
3 COUNT(ord.id) as orders_count
4 FROM my_schema.customers cust
5 JOIN my_schema.orders ord ON cust.id = ord.customer_id
6 WHERE 1=1
7 AND cust.age <= 30
8 GROUP BY cust.id, cust.name
9
Avoid:
Favor:
https://www.java-success.com/15-sql-best-practices-interview-qas/ 6/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
1 SELECT s.id, s.age
2 FROM sales s
3 WHERE s.sale_date between '2024-01-01' AND '2024-12-31'
4
#12. Favor using wildcards at the end of like ‘p%’ as opposed to the
beginning like ‘%d’ as it is faster & more efficient. At times you want
to search by the last numbers as in last 4 digits of a credit card
number or a phone number. In this scenario have an additional
column with the reversed card number or phone number.
LIKE:
1 SELECT *
2 FROM employees
3 WHERE LOWER(name) LIKE '%ep%' OR
4 LOWER(name) LIKE '%es%' OR
5 LOWER(name) LIKE '%em%' OR
6 LOWER(name) LIKE '%et%'
7
REGEXP_LIKE:
1 SELECT *
2 FROM employees
3 WHERE REGEXP_LIKE( LOWER(name), 'ep|es|em|et')
4
1 SELECT id
2 , CASE WHEN LOWER(name) LIKE '%peter%' THEN 'Peter'
3 WHEN LOWER(name) LIKE '%jessica%' THEN 'Jessica'
4 WHEN LOWER(name) LIKE '%john%' THEN 'John'
5 END AS emp_name
6 FROM employees
7
REGEXP:
https://www.java-success.com/15-sql-best-practices-interview-qas/ 7/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
1 SELECT id
2 , CASE WHEN LOWER(name) REGEXP 'peter|john|jessica' THEN CONCA
3 END AS emp_name
4 FROM employees
5
Avoid:
Favor:
LIMIT & TOP clauses are more efficient than COUNT(*) or EXISTS(…),
but can only be used in scenarios like testing or sampling.
#14. Favor UNION ALL over UNION , when you know that the two
tables being combined don’t have any duplicates. The UNION ALL is
more efficient as it does not have to perform a deduplication
operation.
#15. Create joins with INNER JOIN but not with WHERE as joins with
WHERE can create a cartesian product or a CROSS JOIN. In a
CROSS JOIN, all possible combinations of the variables are created.
Avoid:
1 SELECT cust.id,
2 cust.name,
3 COUNT(ord.id) as orders_count
https://www.java-success.com/15-sql-best-practices-interview-qas/ 8/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
4 FROM my_schema.customers cust, my_schema.orders ord
5 WHERE 1=1
6 AND cust.id = ord.customer_id
7 AND cust.age <= 30
8 GROUP BY cust.id, cust.name
9
Correct:
1 SELECT cust.id,
2 cust.name,
3 COUNT(ord.id) as orders_count
4 FROM my_schema.customers cust
5 JOIN my_schema.orders ord ON cust.id = ord.customer_id
6 WHERE 1=1
7 AND cust.age <= 30
8 GROUP BY cust.id, cust.name
9
Avoid:
Favor:
https://www.java-success.com/15-sql-best-practices-interview-qas/ 9/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
1 SELECT s.id, s.age, COUNT(s.ord_id)
2 FROM sales s
3 WHERE s.age < 25
4 GROUP BY s.id, s.age
5
Instead Of:
1 SELECT cust.id,
2 cust.salary,
3 ord.order_no
4 prod.product_code,
5 inv.invoice_no
6 FROM my_schema.customers cust
7 LEFT JOIN my_schema.order AS ord on cust.ord_id = ord.ord_id
8 LEFT JOIN my_schema.product AS prod on cust.product_id = prod
9 LEFT JOIN my_schema.invoice AS inv on order.invoice_id = inv
10 ...
11
#18. Use meaningful concise names for the table & column names.
Most organisations maintain a data dictionary with an abbreviated
naming convention to be adhered to. They also have a review &
approval process in place where the Data assets are reviewed by the
Data modellers, Data stewards, Leads, Product owners, etc.
Instead Of:
1 SELECT COUNT(*)
2 FROM order o
3 JOIN product p
4 ON o.product_id = p.product_id
5 WHERE p.product_code= 'BCL-23'
6
Favor using
1 SELECT EXISTS (
2 SELECT 1 FROM order o
3 JOIN product p
4 ON o.product_id = p.product_id
5 WHERE p.product_code= 'BCL-23'
https://www.java-success.com/15-sql-best-practices-interview-qas/ 11/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
6 )
7
1
2 SELECT product_id
3 FROM product
4 WHERE EXISTS (SELECT product_id FROM orders);
5
1
2 SELECT product_id
3 FROM product
4 WHERE product_id IN (SELECT product_id FROM orders WHERE orde
5
Wrong:
https://www.java-success.com/15-sql-best-practices-interview-qas/ 12/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
Correct:
00: A roadmap to become a Big Data Engineer – What skills are required? ›
Arulkumaran Kumaraswamipillai
Mechanical Engineer to self-taught Java engineer in 1999. Contracting since 2002 as a Java Engineer &
Architect and since 2016 as a Big Data Engineer & Architect. Preparation & key know-hows empowered me
to attend 150+ job interviews, choose from 130+ job offers & negotiate better contract rates. Author of the
book "Java/J2EE job interview companion", which sold 35K+ copies & superseded by this site with 3.5K+
registered users Amazon.com profile | Reviews | LinkedIn | LinkedIn Group | YouTube Email: java-
[email protected]
https://www.java-success.com/15-sql-best-practices-interview-qas/ 13/15
06/12/2024, 19:29 00: 18+ SQL best practices & optimisation interview Q&As | 800+ Big Data & Java Interview FAQs
"You are paid to read & write lots of code & solve business problems in a
collaborative environment"
100+ Free Java Interview FAQs
100+ Free Big Data Interview FAQs
Don't be overwhelmed by the number of Q&As. Job interviews are not technical contests to see who gets most number of questions right.
Nobody knows everything. The Clarity of the answers you give with real-life examples will go a long way in getting you multiple job offers.
It pays to brush-up & choose from 2-6 job offers. Experienced interviewers can easily judge your real experience from a few open-ended
questions & the answers you provide.
1. Feeling stagnated?
2. How to earn more?
3. Freelancing Vs contracting?
4. Self-taught professional?
5. Job Interview Tips
6. Resume Writing Tips
300+ Big Data Interview Q&As with code, scenarios & FAQs
100+ SQL Interview Q&As 01. 50+ SQL scenarios based interview Q&As – What is wrong with this SQL code? 02.…
(2,423)
00: Top 50+ Core Java interview questions & answers for 1 to 3 years experience
Top 50 core Java interview questions covering core Java concepts with diagrams, code, examples, and scenarios. If you don't get…
(1,968)
Java 8 String streams and finding the first non repeated character with functional programming
Q1.Find the first non repeated character in a given string input using Java 8 or later? A1.Extends Find the first…
(1,666)
Membership Levels
Membership prices listed below are in Australian Dollars (A$). If you purchase in other currencies like Indian rupees, the equivalent…
(718)
0: 50+ SQL scenarios based interview Q&As – What is wrong with this SQL code?
This extends 18+ SQL best practices & optimisation interview Q&As. You can practice these SQLs by setting up the data…
(369)
Disclaimer
The contents in this Java-Success are copyrighted and from EmpoweringTech pty ltd. The EmpoweringTech pty ltd has the right to correct or enhance the current content without
any prior notice. These are general advice only, and one needs to take his/her own circumstances into consideration. The EmpoweringTech pty ltd will not be held liable for any
damages caused or alleged to be caused either directly or indirectly by these materials and resources. Any trademarked names or labels used in this blog remain the property of
their respective trademark owners. Links to external sites do not imply endorsement of the linked-to sites. Privacy Policy
© 2024 800+ Big Data & Java Interview FAQs Responsive WordPress Theme powered by CyberChimps
Top
https://www.java-success.com/15-sql-best-practices-interview-qas/ 15/15