Formatting Your Queries

 

Using Upper and Lower Case in SQL:


SQL queries can be run successfully whether characters are written in upper- or lower-case. In other words, SQL queries are not case-sensitive. The following query:

SELECT account_id FROM orders

is the same as:

select account_id from orders

which is also the same as:

SeLeCt AcCoUnt_id FrOm oRdErS

Avoid Spaces in Table and Variable Names


It is common to use underscores and avoid spaces in column names. It is a bit annoying to work with spaces in SQL. In Postgres if you have spaces in column or table names, you need to refer to these columns/tables with double quotes around them (Ex: FROM "Table Name" as opposed to FROM table_name). In other environments, you might see this as square brackets instead (Ex: FROM [Table Name]).


Use White Space in Queries


SQL queries ignore spaces, so you can add as many spaces and blank lines between code as you want, and the queries are the same. This query



SELECT account_id FROM orders


is equivalent to this query:


SELECT account_id FROM orders


and this query (but please don't ever write queries like this):


SELECT account_id FROM orders


Semicolons


Depending on your SQL environment, your query may need a semicolon at the end to execute. Other environments are more flexible in terms of this being a "requirement." It is considered best practice to put a semicolon at the end of each statement, which also allows you to run multiple queries at once if your environment allows this.

Best practice:


SELECT account_id FROM orders;



Since our environment here doesn't require it, you will see solutions written without the semicolon:



SELECT account_id FROM orders








Comments

Popular posts from this blog

Entity Relationship Diagrams

Arithmetic Operators

NOT