SQL SELECT

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.

Example

#

List all suppliers in the UK.

SELECT *
  FROM Supplier
 WHERE Country = 'UK'

Syntax

SELECT syntax.

SELECT column-names
  FROM table-name

To include all columns use *

SELECT *
  FROM table-name

More Examples

SELECT with WILDCARD

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: List all customers.
SELECT * 
  FROM Customer

Note: The * is the wildcard. It means return all columns.

Result:  91 records
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

SELECT with COLUMNS

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: List the first name, last name, and city of all customers.
SELECT FirstName, LastName, City 
  FROM Customer
Result:  91 records
FirstName LastName City
Maria Anders Berlin
Ana Trujillo México D.F.
Antonio Moreno México D.F.
Thomas Hardy London
Christina Berglund Luleå

Developers also Ask


How do I SELECT specific rows in SQL?

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.

Is SELECT a DML command in SQL?

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:

  • INSERT - adds a single or multiple records in the table
  • UPDATE - modifies an existing record
  • DELETE - removes a record from the database

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.

What does SQL stand for?

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).

Are semicolons necessary in SQL Server?

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 .

What is a parameterized query?

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

You may also like



Guides


vsn 3.1