DEV Community

Discussion on: How to write SQL queries which are easier to read?

Collapse
 
bergamin profile image
Guilherme Taffarel Bergamin

God knows how much I hate those implicit joins. You never know what's going on on this kind of queries.

My queries are usually more or less like this:

SELECT A.field1
      ,B.field2
      ,CASE B.field2
            WHEN 1 THEN 'Text 1'
            WHEN 2 THEN 'Text 2'
            ELSE 'Else Text'
       END AS field3
      ,B.field4
  FROM table1      A
 INNER JOIN table2 B
    ON B.id_table1 = A.id
   AND B.field5 = 1
 WHERE A.field6 = 2
   AND A.field7 = 3;
Collapse
 
javinpaul profile image
javinpaul

Sorry, I didn't get your point, isn't join is explicit on my query example?

Collapse
 
bergamin profile image
Guilherme Taffarel Bergamin

It's not a point, I'm just saying I rather do queries the same way you did. I'm not talking about your example, I'm talking about most people I've been working with.

Looks like people are stuck in time sometimes. Huge SQL lines, list of tables instead of JOINs, a lot of business rules in procedures and triggers when they should have been added to the application backend, etc.

Thread Thread
 
javinpaul profile image
javinpaul

Ah, I see from where you are coming. I have seen such queries, and, yes, they are very messy to deal with. I can understand your pain, especially if you have to make a change on such code.