Note the following syntax is for Postgres SQL, although they are all very similar
Before Starting to learn about SQL, know some of the essential characteristics of SQL language that’ll help your learning SQL journey:
-
and extend to the end of the line./*
and end with /
.Select * FROM TABLE_Name
Syntax: can’t have a comma after the last column name
Rename Columns using keyword AS:
SELECT
date AS DAY,
open AS price
FROM
tutorial.aapl_historical_stock_price
LIMIT keyword: limits the rows by the limit you decide at the end of your query
WHERE: Filters the data with the specified conditions
SELECT
date AS DAY,
open AS opening_price
FROM
tutorial.aapl_historical_stock_price
WHERE open > 500
LIMIT 10
Comparison Operators (use in Where clause)
Creating New Columns
/* You can create new columns / constant columns */
SELECT date, high - low AS day_range, 999 as constant_column
FROM tutorial.aapl_historical_stock_price
Note that column values are put in SINGLE Quotes, where as column names should be put in DOUBLE quotes (if putting a space in col name)
SELECT
*
FROM
tutorial.city_populations
WHERE
state > 'NY'