Databases using 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.
|
Basic 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.
|
SQL Aggregation and aliases
|
Use the GROUP BY keyword to aggregate data.
Functions like MIN , MAX , AVERAGE , SUM , COUNT , etc. operate on aggregated data.
Aliases can help shorten long queries. To write clear and readible queries, use the AS keyword when creating aliases.
Use the HAVING keyword to filter on aggregate properties.
|
Joins
|
Use the JOIN command to combine data from two tables—the ON keyword specifies which columns link the tables.
Regular JOIN returns only matching rows. Other join commands provide different behavior, e.g., LEFT JOIN retains all rows of the table on the left side of the command.
All of the commands we have discussed can be run from the command line as well.
|