SQL Patterns in Practice - You're (Probably) Doing it Wrong - Methods for Improving SQL
-
Upload
sean-scott -
Category
Data & Analytics
-
view
131 -
download
1
description
Transcript of SQL Patterns in Practice - You're (Probably) Doing it Wrong - Methods for Improving SQL
SQL Patterns in Practice(You’re Probably Doing it Wrong)
Sean Scott, Oracle [email protected]
The Four Rules of Writing SQL
To improve readability and self-document code, follow these four simple rules.
SELECT order.order_id, order_line, product_name, unit_price, supplier_name, SUM(total_units), sum(unit_price*total_units)FROM order, product, order_items WHERE ((order_items.product_id = product.product_id) and ((order.order_id = order_items.order_id) and (customer_id = 42)))GROUP BY order_id, product_name, unit_price, supplier_name, total_unitsORDER BY order_id, product_name;
Rule 1Everything on its own line
This improves readability and makes troubleshooting easier.
SELECT order.order_id,order_line,product_name,unit_price,supplier_name,SUM(total_units), sum(unit_price*total_units)FROM order,product,order_items WHERE ((order_items.product_id = product.product_id) and((order.order_id = order_items.order_id) and(customer_id = 42)))GROUP BY…
SELECT order.order_id,--order_line,product_name,--unit_price,supplier_name--,--SUM(total_units), --sum(unit_price*total_units)FROM order,product--,--order_items WHERE ((order_items.product_id = product.product_id) and(--(order.order_id = order_items.order_id) andand (customer_id = 42)))
Rule 2Put commas, ANDs at the beginning of lines, not the
endThis minimizes commenting necessary to remove
something from your query.
SELECT order.order_id, order_line, product_name, unit_price, supplier_name, SUM(total_units), sum(unit_price*total_units)FROM order, product, order_items WHERE ((order_items.product_id = product.product_id) and ((order.order_id = order_items.order_id) and (customer_id = 42)))
SELECT order.order_id--, order_line, product_name--, unit_price, supplier_name--, SUM(total_units)--, sum(unit_price*total_units)FROM order, product--, order_items WHERE ((order_items.product_id = product.product_id)--and ((order.order_id = order_items.order_id) ( and (customer_id = 42)))
Rule 3When joining tables, use short, meaningful aliases
for tables and always prefix columns
It eliminates confusion about what table a column belongs to, and improves readability.
SELECT o.order_id, ol.order_line, p.product_name, ol.unit_price, p.supplier_name, SUM(ol.total_units), sum(p.unit_price*ol.total_units)FROM order o, product p, order_items oiWHERE ((oi.product_id = p.product_id) and ((o.order_id = oi.order_id) and (o.customer_id = 42)))
Rule 4Avoid parentheses in
WHERE clauses unless required to nest “OR”
expressionsSimplify SQL by eliminating that which is unnecessary
SELECT o.order_id, ol.order_line, p.product_name, ol.unit_price, p.supplier_name, SUM(ol.total_units), sum(p.unit_price*ol.total_units)FROM order o, product p, order_items oiWHERE oi.product_id = p.product_id and o.order_id = oi.order_id and o.customer_id = 42
Four Rules• Everything on its own line• Put commas, “AND” at the beginning of lines, not
the end• When joining tables, use short, meaningful
aliases for tables and always prefix columns• Avoid parentheses in WHERE clauses unless
required to nest “OR” expressions.
The Six Habits of Legible SQL
Seven habits to adopt when writing SQL that will improve legibility
SELECT o.order_id, ol.order_line, p.product_name, ol.unit_price, p.supplier_name, SUM(ol.total_units), sum(p.unit_price*ol.total_units)FROM order o, product p, order_items oiWHERE oi.product_id = p.product_id and o.order_id = oi.order_id and o.customer_id = 42
Pick a case and stick to it
Upper or lower, it doesn’t matter, provided you’re consistent. Case provides visual cues about the purpose or
meaning of the various parts of your statement.
SELECT o.order_id, ol.order_line, p.product_name, ol.unit_price, p.supplier_name, SUM(ol.total_units), SUM(p.unit_price*ol.total_units)FROM order o, product p, order_items oiWHERE oi.product_id = p.product_id and o.order_id = oi.order_id and o.customer_id = 42
Use white space to align statements for
meaningWhite space makes the parts and purpose of a statement
more visually apparent and easier to read.
SELECT o.order_id, ol.order_line, p.product_name, ol.unit_price, p.supplier_name, SUM(ol.total_units), SUM(p.unit_price*ol.total_units) FROM order o, product p, order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42GROUP BY o.order_id, ol.order_line...
SELECT o.order_id, ol.order_line, p.product_name, ol.unit_price, p.supplier_name, SUM(ol.total_units), SUM(p.unit_price*ol.total_units)FROM order o, product p, order_items oiWHERE oi.product_id = p.product_idAND o.order_id = oi.order_idAND o.customer_id = 42GROUP BY o.order_id, ol.order_line...
Use white space to align operands and
aliasesWhen all operands fall into alignment, it’s much easier to see
the left and right sides in the WHERE statement. Likewise, when table aliases are aligned it’s much easier to reference them.
SELECT o.order_id, ol.order_line, p.product_name, ol.unit_price, p.supplier_name, SUM(ol.total_units), SUM(p.unit_price*ol.total_units) FROM order o, product p, order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42GROUP BY o.order_id, ol.order_line...
Group columns by tableIf you need to troubleshoot by commenting out a table, it’s
more efficient when everything is together.
SELECT o.order_id, p.product_name, p.supplier_name, ol.order_line, ol.unit_price, SUM(ol.total_units), SUM(p.unit_price*ol.total_units) FROM order o, product p, order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42GROUP BY o.order_id, ol.order_line...
In a WHERE clause, equalities first, then IN
lists, and subqueries last.
This orders statements from the least to most likely to cause a problem and need to be edited.
SELECT o.order_id, p.product_name, p.supplier_name, ol.order_line, ol.unit_price, SUM(ol.total_units), SUM(p.unit_price*ol.total_units) FROM order o, product p, order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42GROUP BY o.order_id, ol.order_line...
SELECT o.order_id, p.product_name, p.supplier_name, ol.order_line, ol.unit_price, SUM(ol.total_units), SUM(p.unit_price*ol.total_units) FROM order o INNER JOIN order_items oi ON o.order_id = oi.order_id INNER JOIN product p ON oi.product_id = p.product_id WHERE o.customer_id = 42GROUP BY o.order_id, ol.order_line...
Place aggregate functions last in the
SELECTIt makes the GROUP BY easier to write—just copy/paste the
SELECT clause up to the aggregates as the GROUP BY.
SELECT o.order_id, p.product_name, p.supplier_name, ol.order_line, ol.unit_price, SUM(ol.total_units), SUM(p.unit_price*ol.total_units) FROM order o, product p, order_items oi WHERE oi.product_id = p.product_id AND o.order_id = oi.order_id AND o.customer_id = 42GROUP BY o.order_id, ol.order_line...
Six Habits of Legible SQL
• Pick a case and stick to it• Use white space to align statements for meaning• Use white space to align operands and aliases• Group columns by table• In a WHERE clause, equalities first, then IN lists,
and subqueries last• Place aggregate functions last in the SELECT