# My SQL Bad Habits

DevFeed: [My SQL Bad Habits](<https://devfeed.tech/articles/my-sql-bad-habits-41139.md>)

Original publisher: [Read original article](<https://www.craigkerstiens.com/2013/05/26/My-SQL-Bad-Habits/>)

Author: Map

Published: 2013-05-26T20:55:56Z

Content type: opinion

Language: en

Sources: [Craig Kerstiens](<https://devfeed.tech/sources/craig-kerstiens.md>)

Topics: [SQL](<https://devfeed.tech/topics/sql.md>), [syntax](<https://devfeed.tech/topics/syntax.md>), [JOIN](<https://devfeed.tech/topics/join.md>), [ordering](<https://devfeed.tech/topics/ordering.md>)

Tags: [implicit-joins](<https://devfeed.tech/tags/implicit-joins.md>), [ordering](<https://devfeed.tech/tags/ordering.md>), [postgres](<https://devfeed.tech/tags/postgres.md>), [query](<https://devfeed.tech/tags/query.md>), [sql](<https://devfeed.tech/tags/sql.md>), [syntax](<https://devfeed.tech/tags/syntax.md>), [tables](<https://devfeed.tech/tags/tables.md>)

## AI overview

The author discusses personal SQL bad habits that can make queries less clear or more error-prone. The article covers ordering or grouping by column numbers, implicit joins, insufficient comments, and manually generated lists used for filtering.

## Source excerpt

I'm reasonably proficient at SQL - a coworker when pseudocoding some logic for him pointed out that my pseudocode is what he thought was executable SQL. I'm fully capable of writing clear and readable SQL - which most SQL is not. Despite that I still have several bad habits when it comes to SQL. Without further adieu heres some of my dirty laundry so hopefully others can not make the same mistakes. Order/Group by Column Numbers When quickly iterating on a query its a lot less typing to put the column number as the thing you want to order by. Here's a quick lightweight example: SELECT email, created_at FROM users ORDER BY 2 DESC LIMIT 5; This gives me my last 5 users that have signed up for my site. Of course as soon as I have this I may want to add some data to it, like their first name so I can send them a welcome email. I quickly alter the query to: SELECT email, first_name, created_at FROM users ORDER BY 2 DESC LIMIT 5; And now I have 5 users that have signed up ordered by their first name. Sure its obvious when you have 1 column you're ordering by, but when you have GROUP BY 1, 2, 3, 4, 5, 6 which is actually open in one of my tabs currently its a bit more confusing.... Though if you really want to have some fun, share a query with someone that looks something like this: SELECT email as "3", first_name "2", created_at "1" FROM users ORDER BY "1", "3" DESC LIMIT 5; Implicit Joins I seldom use the syntax INNER JOIN. Instead I simply put the two tables in my where clause and ensure I have a where condition. The problem with ensuring I have a where condition is sometimes I don't, especially when you're dealing with 3 tables. SELECT email, product.name, product.price FROM users, orders, items WHERE users.id = orders.user_id AND orders.id = items.order_id Is less clear (especially when dealing with 5-6 tables) than the alternative: SELECT email, product.name, product.price FROM users INNER JOIN orders on users.id = orders.user_id INNER JOIN items on orders.id = items.or