SQL 筆記
SQL is a programming language designed to manipulate and manage data stored in relational databases.
- 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.
A statement is a string of characters that the database recognizes as a valid command.
CREATE TABLEcreates a new table.
INSERT INTOadds a new row to a table.
SELECTqueries data from a table.
ALTER TABLEchanges an existing table.
UPDATEedits a row in a table.
DELETE FROMdeletes rows from a table.
Constraints add information about how a column can be used.
SELECTis the clause we use every time we want to query information from a database.
ASrenames a column or table.
DISTINCTreturn unique values.
WHEREis a popular command that lets you filter the results of the query based on conditions that you specify.
LIKEandBETWEENare special operators.
ANDandORcombines multiple conditions.
ORDER BYsorts the result.
LIMITspecifies the maximum number of rows that the query will return.
CASEcreates different outputs.
COUNT(): count the number of rows
SUM(): the sum of the values in a column
MAX()/MIN(): the largest/smallest value
AVG(): the average of the values in a column
ROUND(): round the values in the column
Aggregate functions combine multiple rows together to form a single value of more meaningful information.
GROUP BYis a clause used with aggregate functions to combine data from one or more columns.
HAVINGlimit the results of a query based on an aggregate property.
JOINwill combine rows from different tables if the join condition is true.
LEFT JOINwill return every row in the left table, and if the join condition is not met,NULLvalues are used to fill in the columns from the right table.
- Primary key is a column that serves a unique identifier for the rows in the table.
- Foreign key is a column that contains the primary key to another table.
CROSS JOINlets us combine all rows of one table with all rows of another table.
UNIONstacks one dataset on top of another.
WITHallows us to define one or more temporary tables that can be used in the final query.
https://www.codecademy.com/learn/learn-sql/modules/learn-sql-manipulation/cheatsheet