Introducing Databases and SQL
- SQL allows us to select and group subsets of data, do math and other calculations, and combine data.
- A relational database is made up of tables which are related to each other by shared keys.
- Different database management systems (DBMS) use slightly different vocabulary, but they are all based on the same ideas.
Accessing Data With Queries
- It is useful to apply conventions when writing SQL queries to aid readability.
- Use logical connectors such as AND or OR to create more complex queries.
- Calculations using mathematical symbols can also be performed on SQL queries.
- Adding comments in SQL helps keep complex queries understandable.
Aggregating and Grouping Data
- Use the
GROUP BYkeyword to aggregate data. - Functions like
MIN,MAX,AVG,SUM,COUNT, etc. operate on aggregated data. - Aliases can help shorten long queries. To write clear and readable
queries, use the
ASkeyword when creating aliases. - Use the
HAVINGkeyword to filter on aggregate properties. - Use a
VIEWto access the result of a query as though it was a new table.
Combining Data With Joins
- Use a
JOINclause to combine data from two tables—theONorUSINGkeywords specify which columns link the tables. - Regular
JOINreturns only matching rows. Other join clauses provide different behavior, e.g.,LEFT JOINretains all rows of the table on the left side of the clause. -
COALESCEallows you to specify a value to use in place ofNULL, which can help in joins -
NULLIFcan be used to replace certain values withNULLin results - Many other functions like
COALESCEandNULLIFcan operate on individual values.