The SQL SELECT statement retrieves data from a database.
Data is returned in a table-like structure called a result-set.
SELECT is the most commonly used operation in a database.
List all suppliers in the UK.
SELECT *
FROM Supplier
WHERE Country = 'UK'
SELECT syntax.
SELECT column-names FROM table-name
To include all columns use *
SELECT * FROM table-name
CUSTOMER |
---|
Id |
FirstName |
LastName |
City |
Country |
Phone |
SELECT *
FROM Customer
Note: The * is the wildcard. It means return all columns.
Id | FirstName | LastName | City | Country | Phone |
---|---|---|---|---|---|
1 | Maria | Anders | Berlin | Germany | 030-0074321 |
2 | Ana | Trujillo | México D.F. | Mexico | (5) 555-4729 |
3 | Antonio | Moreno | México D.F. | Mexico | (5) 555-3932 |
4 | Thomas | Hardy | London | UK | (171) 555-7788 |
5 | Christina | Berglund | Luleå | Sweden | 0921-12 34 65 |
CUSTOMER |
---|
Id |
FirstName |
LastName |
City |
Country |
Phone |
SELECT FirstName, LastName, City
FROM Customer
FirstName | LastName | City |
---|---|---|
Maria | Anders | Berlin |
Ana | Trujillo | México D.F. |
Antonio | Moreno | México D.F. |
Thomas | Hardy | London |
Christina | Berglund | Luleå |
To select specific rows use the WHERE clause which filters data that meet certain conditions. Here is the syntax:
SELECT column-names FROM table-name WHERE conditions
column-names
- a list of column names
table-name
- the name of the table
conditions
- the criteria which rows to include
The WHERE
conditions can include the LIKE keyword which matches words against a pattern.
Other options include the IN,
IS NULL, and
BETWEEN keywords.
Multiple row conditions can be combined by creating AND,
OR, and
NOT logical conditions.
Commands in SQL fall into 5 different categories -- one of which is DML (Data Manipulation Language). The DML commands are used to create, modify or delete data in the database. These include:
The SELECT
command falls into its own category: DQL (Data Query Language).
This category only has a single command, but it is by far the most commonly used query.
SQL stands for Structured Query Language.
It is pronounded "sequel" or SQL (three letters).
SQL is used to access and manipulate data in relational databases.
SQL was developed in the 70's. Today it is a standard maintained by the American National Standards Institute (ANSI).
The semicolon (;) is used as a statement terminator.
They are often used in T-SQL with functions, procedures, batches, etc.
In most cases semicolons are not required to run a query.
However, Microsoft has announced that, in future versions of SQL Server, semicolons may become mandatory .
A parameterized query is a statement with parameters.
These parameters are placeholders for parameter values.
The main reason to use parameterized queries is to avoid SQL injection attacks.
Here is an example of a parameterized SELECT
statement.
It has a parameter named @LastName
which accepts a LastName value.
SELECT FirstName, LastName, City FROM Customer WHERE LastName = @LastName