A relational database is a database that organizes information into one or more tables.
A table is a collection of data organized into rows and columns. Tables are sometimes referred to as relations.
CREATE TABLE: creates a new table
INSERT INTO: adds a new row to a table
SELECT: queries data from a table
UPDATE: edits a row in a table
ALTER TABLE: edits a new column to a table
DELETE FROM: deletes rows from a table
WHERE: filters the results of the query based on conditions that you specify.
LIKE:is used with WHERE to specify data format.
BETWEEN: is used with WHERE to specify data range
AND & OR are special operators that you can use with WHERE to filter the query on two or more conditions
ORDER BY: sorts the results of the query in either ascending or descending order.
LIMIT: specifies the maximum number of rows that a query will return.
COUNT: takes the name of a columns(s) as an argument and counts the number of rows where the value(s) is not null.
GROUP BY: combines data from one or more columns.
SUM():takes the column name as an argument and returns the sum of all the values in that column.
MAX(): takes the column name as an argument and returns the largest value in that column.
MIN():takes the column name as an argument and returns the smallest value in that column.
AVG():takes the column name as an argument and returns the average value in that column.
ROUND():takes two arguments, a column name and the number of decimal places to round the values in that column.
A foreign key is a column that contains the primary key of another table in the database. We use foreign keys and primary keys to connect rows in two different tables. One table's foreign key holds the value of another table's primary key. Unlike primary keys, foreign keys do not need to be unique and can be NULL.
Primary Key is a column that serves a unique identifier for row in the table. Values in this column must be unique and cannot be NULL
.
Joins are used in SQL to combine data from multiple tables.
INNER JOIN: combines rows from different tables if the join condition is true.
An example:
SELECT * FROM albums
JOIN artists ON
albums.artist_id = artists.id;
LEFT OUTER JOIN:returns every row in the lefttable, and if the join condition is not met, NULL
values are used to fill in the columns from the righttable.
An example:
SELECT *FROM albums
LEFT JOIN artists ON albums.artist_id = artists.id;
AS: renames a column or table in the result set using an alias.
An example:
SELECT
albums.name AS 'Album',
albums.year,
artists.name AS 'Artist'
FROM albums
JOIN artists ON albums.artist_id = artists.id
WHERE albums.year > 1980;
网友评论