SQL Query Performance Optimisation

Try not to use SELECT * and provide specific fields Bad Example 1 SELECT * FROM user; Good Example 1 SELECT id, username, tel FROM user; Reason Specify fields will save computational resources, and reduce network expense. It’s possible using pre-built table index to reduce table returns, and improve query efficiency. Avoid using OR in the where clause to connect conditions Bad Example 1 2 SELECT id, username, tel FROM user WHERE id=1 OR salary=500; Good Example Use UNION ALL to concatenate results 1 2 3 4 5 SELECT id, username, tel FROM user WHERE id=1 UNION ALL SELECT id, username, tel FROM user WHERE salary=500; Use two separate SQL 1 2 SELECT id, username, tel FROM user WHERE id=1 1 2 SELECT id, username, tel FROM user WHERE salary=500; Reason The use of OR may invalidate the table index, and thus will do full table scan....

August 11, 2013 · 4 min · 673 words · Eric

Advanced SQL Concepts

Query Execution Order Most people would write their SQL queries starting from SELECT part, because it’s more intuitive and close to our natural language. But actually that’s not the way that SQL queries been executed in query engine. Below is the execution order of a SQL query: FROM, JOIN. Tables are joined to get the base data. WHERE. The base data is filtered. GROUP BY. The filtered based data is grouped....

March 14, 2013 · 2 min · 386 words · Eric

Interview Preparation for SQL Questions

Resource Zachary Thomas’ SQL Questions: https://lnkd.in/g-JJzuD Select * SQL: https://selectstarsql.com/ Leetcode: https://lnkd.in/g3c5JGC LinkedIn Learning: https://lnkd.in/gQXFc4n Window Functions: https://lnkd.in/g3RtPCJ HackerRank: https://lnkd.in/grv_9sB W3 Schools: https://lnkd.in/gJPfrrv CodeAcademy: https://lnkd.in/gT5xmpN SQLZoo: https://sqlzoo.net/ SQL Bolt: https://sqlbolt.com/ FreeCodeCamp: SQL Tutorial - Full Database Course for Beginners

March 14, 2013 · 1 min · 39 words · Eric

Basic SQL Concepts

Join Full Join Inner Join Left Join Right Join Aggregation Function COUNT(). MAX(). MIN(). SUM(). AVG(). The GROUP BY statement groups rows that have the same values into summary rows, like ‘find the number of customers in each country’. The GROUP BY statement is often used with above aggregation functions to group the result-set by one or more columns.

February 14, 2013 · 1 min · 59 words · Eric