Formatting
There is some flexibility to how you format your queries.
Case-Insensitivity
You've noticed that the SQL queries I show you have some capital and some lowercase letters. I have been capitalizing keywords, and lowercasing everything else. This isn't required. SQL keywords and identifiers are case-insensitive. It's a convention a lot of people follow, a stylistic choice. These 2 queries are equivalent:
SELECT *
FROM cars
WHERE model = 'cr-v';
select *
from CARS
where MODEL = 'cr-v';
id INTEGER | make VARCHAR | model VARCHAR | year INTEGER | color VARCHAR | asking_price_usd INTEGER | miles_driven NUMERIC | is_paid_off BOOL |
---|---|---|---|---|---|---|---|
53953 | honda | cr-v | 2006 | black | 3900 | 129853.7 | TRUE |
Text Values Are Case-Sensitive!
I just showed you that SQL keywords and identifiers (names of tables, columns, functions) are case-insensitive. However, text values are not! They are case-sensitive, i.e. upper/lower-case matters.
SELECT *
FROM cars
WHERE make = 'audi';
id INTEGER | make VARCHAR | model VARCHAR | year INTEGER | color VARCHAR | asking_price_usd INTEGER | miles_driven NUMERIC | is_paid_off BOOL |
---|---|---|---|---|---|---|---|
76898 | audi | a6 | 2007 | black | 4500 | 18754.1 | TRUE |
SELECT *
FROM cars
WHERE make = 'Audi';
id INTEGER | make VARCHAR | model VARCHAR | year INTEGER | color VARCHAR | asking_price_usd INTEGER | miles_driven NUMERIC | is_paid_off BOOL |
---|---|---|---|---|---|---|---|
no rows |
Whitespace
The amount of space between words doesn't matter. If you have 1 space or 10, there is no difference. Line breaks are also treated the same as normal spaces. These 2 queries are equivalent:
SELECT * FROM cars WHERE model='explorer';
SELECT *
FROM cars
WHERE model = 'explorer';
id INTEGER | make VARCHAR | model VARCHAR | year INTEGER | color VARCHAR | asking_price_usd INTEGER | miles_driven NUMERIC | is_paid_off BOOL |
---|---|---|---|---|---|---|---|
86699 | ford | explorer | 2005 | red | 1700 | 156000.0 | TRUE |
How you choose to format your queries is up to you and your team. In general, try to write queries that are easily readable and understandable by others. Spacing can sometimes help with that.
Comments
Speaking of writing understandable queries, sometimes you just need to explain your code in plain English.
You can add a comment, which is a part of the query that is ignored by the database.
It is purely for readers to understand something about the code.
Make a line into a comment by adding 2 hyphens --
in front.
SELECT *
FROM cars
WHERE
-- my mom drives a Honda, so I don't want one
make != 'honda'
-- cars with liens can have legal complications. not worth the hassle!
AND is_paid_off
AND asking_price_usd < 4000;
id INTEGER | make VARCHAR | model VARCHAR | year INTEGER | color VARCHAR | asking_price_usd INTEGER | miles_driven NUMERIC | is_paid_off BOOL |
---|---|---|---|---|---|---|---|
86699 | ford | explorer | 2005 | red | 1700 | 156000.0 | TRUE |
59768 | acura | tl | 2004 | gold | 3000 | 166450.5 | TRUE |
14167 | nissan | altima | 1997 | silver | 1500 | 126000.0 | TRUE |
All the queries we've seen so far are pretty simple, so they are generally understood without much explanation. They would just read the query and know what it does. Comments become more crucial when the queries get really complex.