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;