I am working on SQL bridge tables. How do I understand this query? I don't understand all of the letters. Thanks a bunch. Josh
The letters are "aliases" and save the programmer from having to repeat the table name, and allows them to use a table twice in the same query therefore select d.Build_name as 'Building', b.type as 'Energy Type', a.category as 'Energy Category' from EnergyCategories as a... Code (markup): could also be written as select Buildings.Build_name as 'Building', EnergyTypes.type as 'Energy Type', EnergyCategories.category as 'Energy Category' from EnergyCategories... Code (markup): The left joins describe how the tables relate to each other.
Lets start with whoever wrote the query isn't an expert... From my code examples above I'd prefer to see something like this: SELECT b.Build_name, et.type, ec.category FROM EnergyCategories AS ec LEFT JOIN EnergyTypes AS et ON ec.cat_id = et.cat_id LEFT JOIN BETypes AS bet ON et.type_id = bet.type_id LEFT JOIN Buildings AS b ON bet.Build = b.Build WHERE ec.category = 'Renewable' Code (markup): So what I've done is make the alias (short name) match the table name. a, b, c , d is nonsense. ec, et, bet, b make sense if you know the tables that you're working with. You should be able to read the query and intuitively know what the alias is referring to. You're new to this so it may still be hard now, but it will get easier. I've also removed the alias names for the fields you're selecting. I usually only use them on aggregated fields (count, case, etc) or where I'm selecting two fields with the same name (id, name, notes, etc) - or if I'm passing the results to another tool like a csv generator that will use the field names as column headings.