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.
SELECT and FROM
The SELECT part of a query determines which columns of the data to show in the results. There are also options you can apply to show data that is not a table column.
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.
CREATE TABLE does 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.
changes the structure of a table. Here is how you would add a column to a database:
ALTER TABLE table_name
ADD column_name datatype;
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.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')
);
WHERE
(AND, OR, IN, BETWEEN, and LIKE)
The WHERE clause is used to limit the number of rows returned.
As an example, first we will show you a SELECT statement and results without a WHERE statement. Then we will add a WHERE statement that uses all five qualifiers above.
SELECT studentID, FullName, sat_score, rcd_updated FROM student;
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.
SELECT studentID, FullName, sat_score
FROM student
ORDER BY FullName DESC;
COUNT
COUNT will count the number of rows and return that count as a column in the result set.
Here are examples of what you would use COUNT for:
Counting all rows in a table (no group by required)
Counting the totals of subsets of data (requires a Group By section of the statement)
This SQL statement provides a count of all rows. Note that you can give the resulting COUNT column a name using “AS”.
SELECT count(*) AS studentCount FROM student;
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:
DELETE FROM table_name
WHERE condition;
Here is an example deleting from the table Person the record with Id 3:
DELETE FROM Person
WHERE Id = 3;
INNER JOIN
JOIN, also called Inner Join, selects records that have matching values in two tables.
SELECT * FROM A x JOIN B y ON y.aId = x.Id
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (