New DOC Document
New DOC Document
Beau Carnes
SQL stands for Structured Query Language. SQL commands are the
instructions used to communicate with a database to perform tasks, functions,
and queries with data.
SQL commands can be used to search the database and to do other functions
like creating tables, adding data to tables, modifying data, and dropping tables.
Here is a list of basic SQL commands (sometimes called clauses) you should
know if you are going to work with SQL.
The example below shows three columns SELECTed FROM the “student” table and
one calculated column. The database stores the studentID, FirstName, and
LastName of the student. We can combine the First and the Last name columns
to create the FullName calculated column.
+-----------+-------------------+------------
+------------------------+
| studentID | FirstName | LastName | FullName
|
+-----------+-------------------+------------
+------------------------+
CREATE TABLE
CREATE TABLEdoes just what it sounds like: it creates a table in the database. You
can specify the name of the table and the columns that should be in the table.
column_1 datatype,
column_2 datatype,
column_3 datatype);
ALTER TABLE
ALTER TABLE changes the structure of a table. Here is how you would add a
column to a database:
CHECK
The CHECK constraint is used to limit the value range that can be placed in a
column.
If you define a CHECK constraint on a single column it allows only certain values
for this column. If you define a CHECK constraint on a table it can limit the values
in certain columns based on values in other columns in the row.
The following SQL creates a CHECK constraint on the “Age” column when the
“Persons” table is created. The CHECK constraint ensures that you can not have
any person below 18 years.
FirstName varchar(255),
Age int,
CHECK (Age>=18));
FirstName varchar(255),
Age int,
City varchar(255),
WHERE
(AND, OR, IN, BETWEEN, and LIKE)
+-----------+------------------------+-----------
+---------------------+
+-----------+------------------------+-----------
+---------------------+
+-----------+------------------------+-----------
+---------------------+
Now, we'll repeat the SELECT query but we'll limit the rows returned using
a WHERE statement.
AND
+-----------+----------------------+-----------+---------------------
+
+-----------+----------------------+-----------+---------------------
+
| 1 | Monique Davis | 400 | 2017-08-16 15:34:50
|
+-----------+----------------------+-----------+---------------------
+
UPDATE
To update a record in a table you use the UPDATE statement.
Use the WHERE condition to specify which records you want to update. It is
possible to update one or more columns at a time. The syntax is:
GROUP BY
GROUP BY allows you to combine rows and aggregate data.
HAVING
HAVING allows you to filter the data aggregated by the GROUP BY clause so that the
user gets a limited set of records to view.
AVG()
“Average” is used to calculate the average of a numeric column from the set of
rows returned by a SQL statement.
AS
AS allows you to rename a column or table using an alias.
+-------------+------------------------+
| AgeOfServer | NonWarrantyPeriod |
+-------------+------------------------+
| 36 | 24 |
| 24 | 12 |
| 61 | 49 |
| 12 | 0 |
| 6 | -6 |
| 0 | -12 |
| 36 | 24 |
| 36 | 24 |
| 24 | 12 |
+-------------+------------------------+
You can also use AS to assign a name to a table to make it easier to reference
in joins.
+-------------+------------+-----------+-----------------
+--------------+
+-------------+------------+-----------+-----------------
+--------------+
+-------------+------------+-----------+-----------------
+--------------+
ORDER BY
ORDER BY gives us a way to sort the result set by one or more of the items in
the SELECT section. Here is an SQL sorting the students by FullName in
descending order. The default sort order is ascending (ASC) but to sort in the
opposite order (descending) you use DESC.
COUNT
COUNT will count the number of rows and return that count as a column in the
result set.
DELETE
DELETE is used to delete a record in a table.
Be careful. You can delete all records of the table or just a few. Use
the WHERE condition to specify which records you want to delete. The syntax is:
Here is an example deleting from the table Person the record with Id 3:
INNER JOIN
JOIN, also called Inner Join, selects records that have matching values in two
tables.
LEFT JOIN
A LEFT JOIN returns all rows from the left table, and the matched rows from the
right table. Rows in the left table will be returned even if there was no match in
the right table. The rows from the left table with no match in the right table will
have null for right table values.
RIGHT JOIN
A RIGHT JOIN returns all rows from the right table, and the matched rows from
the left table. Opposite of a left join, this will return all rows from the right
table even where there is no match in the left table. Rows in the right table that
have no match in the left table will have null values for left table columns.
INSERT
INSERT is a way to insert data into a table.
LIKE
LIKE is used in a WHERE or HAVING (as part of the GROUP BY) to limit the selected rows
to the items when a column has a certain pattern of characters contained in it.
This SQL will select students that have FullName starting with “Monique” or
ending with “Greene”.
+-----------+---------------+-----------+---------------------+
+-----------+---------------+-----------+---------------------+
+-----------+---------------+-----------+---------------------+
You can place NOT before LIKE to exclude the rows with the string pattern instead
of selecting them. This SQL excludes records that contain “cer Pau” and “Ted”
in the FullName column.
+-----------+----------------------+-----------+---------------------
+
+-----------+----------------------+-----------+---------------------
+
+-----------+----------------------+-----------+---------------------
+