SQL SELECT Statement
Select statements are used to select data from a table.
The result is stored in a result table, called a result set.
SQL SELECT Syntax
SELECT column name from table name
And:
SELECT * FROM table name
Note: The SQL statement is not case sensitive. Select is equivalent to select.
To get the contents of a column named "LastName" and "FirstName" (from a database table named "Persons"), use a SELECT statement like this:
SELECT Lastname,firstname from Persons
*
From Persons
Tip: An asterisk (*) is a shortcut to select all columns.
SQL SELECT DISTINCT Statement
In the table, duplicate values may be included. That's not a problem, but sometimes you might want to just list different values (distinct).
Keyword DISTINCT is used to return only different values.
Grammar:
SELECT DISTINCT column name from table name
The WHERE clause is used to specify the criteria for selection.
WHERE clause
To conditionally select data from a table, you can add a WHERE clause to the SELECT statement.
Grammar
SELECT column name from table name WHERE column operator value
If you want to select only people who live in the city "Beijing", we need to add a WHERE clause to the SELECT statement:
WHERE City=‘Beijing‘
Use of quotation marks
Note that we use single quotes around the condition values in the example.
SQL uses single quotation marks to wrap text values (most database systems also accept double quotes). If it is a numeric value , do not use quotation marks.
And and OR operators
And and or can combine two or more conditions in a where sub-statement.
If both the first condition and the second condition are true, the AND operator displays a record.
If only one of the first and second conditions is true, the OR operator displays a record.
AND
Lastname= ' Carter '
Combine and and OR operators
We can also combine and and or together (using parentheses to form complex expressions):
(
OR
Firstname= ' William ' )
AND
lastname= ' Carter '
ORDER by statement
The order BY statement is used to sort the result set based on the specified column.
The order BY statement sorts records by default in ascending order.
If you want to sort records in descending order, you can use the DESC keyword.
Show company name in alphabetical order:
ORDER BY Company
Show company name in reverse alphabetical order:
ORDER BY Company DESC
INSERT into statement
The INSERT INTO statement is used to insert a new row into the table.
Grammar
INSERT into table name values (value 1, value 2,....)
We can also specify the columns for which you want to insert data:
INSERT into table_name (column 1, column 2,...) Values (value 1, value 2,....)
Update statement
The Update statement is used to modify the data in the table.
Grammar:
UPDATE table name SET column name = new value WHERE Column name = value
To update a column in a row
We add FirstName for LastName who is "Wilson":
To update several columns in a row
We will modify the address and add the city name:
UPDATE person SET Address = ' Zhongshan ", city = ' nanjing ' WHERE LastName = ' Wilson '
DELETE statement
The DELETE statement is used to delete rows in a table.
Grammar
DELETE from table name WHERE column name = value
Delete a row
"Fred Wilson" will be removed:
Delete all rows
You can delete all rows without deleting the table. This means that the structure, properties, and indexes of the table are complete:
DELETE from table_name
Or:
DELETE * FROM table_name
SQL Basic Syntax