SQL syntax
Included here are some notes I jotted down while watching this SQL tutorial from freeCodeCamp. Kudos to freeCodeCamp, which offers the best free resources to self-taught developers. - Haowen Jiang
Data types
Creating tables
- Create a column in a table
- Delete a column from a table
Inserting data
Updating and deleting data
- Update all instances of one value with another under a condition
- Update all instances of two values with one under a condition
- Update multiple columns of a row under a condition
Basic queries
- Select specific columns (another way)
- Order by a column in ascending order (by default)
- Order by a column in descending order
- Order by multiple columns
- Limit the results by a number
- Filter the results by a comparison operator
- Filter the results by comparing against a list
- Give an alias to a column
- Select distinct values in a column
Built-in functions
The schema for the company database

COUNT()
- Count the number of rows in a column
- Count the number of female employees born after 1970
- Count the number of male and female employees
AVG()
- Find the average salary of male employees
SUM()
- Find the total salary of employees
- Find the total sales of each employee
Wildcards
-
%
: for any number of characters, equivalent to *
in Regex
-
_
: for a single character, equivalent to .
in Regex
-
Find any employee whose last name ends with son
- Find any employee born in September
Union
Join
Inner join
- Find all branches and the names of their managers, and include only employees that manage a branch
Left join
- Find all branches and the names of their managers, and include all employees
Right join
- Find all branches and the names of their managers, and include all branches
Nested queries
- Find names of all employees who have sold over 30K to a single client
Trigger
- Create a trigger in the SQL terminal
-
What the trigger above does is that whenever a new row is inserted into the employee
table, a fixed message will be inserted into the trigger_test
table.
-
Another trigger, but with more conditions
-
What the trigger above does is that whenever a new row is inserted into the employee
table, a different message will be inserted into the trigger_test
table depending on the sex of the newly inserted employee.
-
Delete a trigger in the SQL terminal
Entity relationship diagrams

- Entity: e.g.
student
- Weak entity: an entity that cannot be uniquely identified by its attributes alone, e.g.
exam
, which is not independent from class
- Attribute: features about an entity, e.g.
student_id
, gpa
- Composite attribute: attribute that consists of multiple attributes, e.g.
name
consisting of last_name
and first_name
- Multi-valued attribute: attribute that has multiple values, e.g.
clubs
, whose values might be guitar
, dancing
, etc.
- Derived attribute: attribute that can be derived from other attributes, e.g.
has_honors
, which can be derived from gpa
- Relationship: a relationship between two entities, eg.
student
holds the relationship takes
with class
.
- Partial participation: e.g. only some students take a specific class
- Total participation: e.g. all classes are taken by at least a student
- Relationship attribute: attribute about a relationship, e.g.
grade
in the takes
relationship between student
annd class
- Relationship cardinality:
- one-to-one: 1:1
- one-to-many: 1:N
- many-to-one: N:1
- many-to-many: N:M
- Identifying relationship: a relationship that serves to uniquely identify a weak entity, e.g.
has
between class
, a normal entity, and exam
, a weak entity
Postscript
Not until I finished writing the notes above did I find this page, where Mike Dane, the instructor, already created a nice-looking page for all the SQL syntax covered in this tutorial.