SELECT 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 column-names FROM table-name
To include all columns use *
SELECT * FROM table-name
SELECT * FROM Customer
Note: The * is the wildcard. It means return all columns.
|2||Ana||Trujillo||México D.F.||Mexico||(5) 555-4729|
|3||Antonio||Moreno||México D.F.||Mexico||(5) 555-3932|
|5||Christina||Berglund||Luleå||Sweden||0921-12 34 65|
SELECT FirstName, LastName, City FROM Customer
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
WHERE conditions can include the LIKE keyword which matches words against a pattern.
Other options include the IN,
IS NULL, and
Multiple row conditions can be combined by creating 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:
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
It has a parameter named
@LastName which accepts a LastName value.
SELECT FirstName, LastName, City FROM Customer WHERE LastName = @LastName