Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data...

11
|SQL-Server Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved. Select query in SQL Server The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables can be achieved through joins. Syntax Here is the syntax for select statement in SQL Server However, the full syntax for the SELECT statement in SQL Server (Transact-SQL) is: SELECT expressions FROM tables [WHERE conditions]; SELECT [ ALL | DISTINCT] [ TOP (top_value) [ PERCENT] [ WITH TIES]] expressions FROM tables [WHERE conditions] [GROUP BY expressions] [HAVING condition] [ORDER BY expression [ ASC | DESC]];

Transcript of Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data...

Page 1: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Select query in SQL Server

The SQL Server SELECT statement is used to retrieve data from one

or more tables in SQL Server database.

Retrieving data from multiple tables can be achieved through joins.

Syntax

Here is the syntax for select statement in SQL Server

However, the full syntax for the SELECT statement in SQL Server

(Transact-SQL) is:

SELECT expressions

FROM tables

[WHERE conditions];

SELECT [ ALL | DISTINCT]

[ TOP (top_value) [ PERCENT] [ WITH TIES]]

expressions

FROM tables

[WHERE conditions]

[GROUP BY expressions]

[HAVING condition]

[ORDER BY expression [ ASC | DESC]];

Page 2: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Parameters or Arguments:

ALL: It is an optional, that returns all matching rows. This can be

replaced by * to fetch all data from the table.

DISTINCT: This also optional. It removes duplicates from the result set.

TOP (top_value): TOP (top_value) is an optional. If it is specified, Top

will return the top number of rows in the result set based on top_value.

For example, TOP (5) will return top 5 records from the full result set.

PERCENT: Optional. If specified, particular percentage of records from

the total records will be retrieved. For example, TOP (5) PERCENT would

return the top 5% of the full result set. If we got 20 rows in a table. 5%

of 20 records will be 1. So, 1 record will be retrieved.

WITH TIES: Optional. If we are looking for top 2 records in ascending

order from the table. I got the data as 10,30,20,20,40. In this case, the

top 2 values in ascending order should be 10,20 but 20 repeats 2 times

indicates with ties option enables the output of 10,20,20 (3 data for top

2 records expected).

expressions: The columns of the table which we need to retrieve.

Page 3: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

tables: In general, the table from which the data needs to be retrieved.

WHERE conditions: WHERE condition is an optional. The conditions

specified by the user to filter the data while fetching the data from the

table.

GROUP BY expressions: Optional. Group the data based on specific

columns.

HAVING condition: Optional. Used in the combination with the

GROUP BY to restrict the groups of returned rows to only those rows

where the condition is TRUE in HAVING clause.

ORDER BY expression: Optional. Order by clause enables the select

statement to sort down the results in ascending (order by

column_Name asc) or descending (order by column_name desc).

Example - Select all fields from one table:

Let's look at how to use a SQL Server SELECT query to select all fields

from a table.

Page 4: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Syntax:

Ex: select * from dbo.Customers

In this SQL Server SELECT statement example, * indicates fetching all

the columns in the customers table.

Example - Select individual fields from one table:

We can also use the SQL Server SELECT statement to select individual

fields from the table, as opposed to all fields from the table.

Syntax:

SELECT * FROM table_name

WHERE {condition]

SELECT

column_name1,column_name2

FROM table_name

[WHERE condition]

Page 5: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Ex: select customerid from dbo.Customers

This SQL Server SELECT example will return only customerid column

and it won’t retrieve other columns from the customers table.

Sample queries:

select * from dbo.Customers

-- dbo.Customers table name selects and fetches all the data from

customers table

select customerid from dbo.Customers

--customerid specifies to fetch only customerid column from the

dbo. Customers table.

Select customerid,city from dbo.Customers where

customerid = 2

-- Here data satisfying the condition customerid=2. Number can be

specified directly without any single quotes.

select customerid,city from dbo.Customers where

city='Chicago'

Page 6: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

-- Here in this example City satisfying the condition it should be

Chicago. String should be enclosed in single quotes as above ‘ ‘ .

Code Explanation:

Here in this example all column is retrieved with * in the select

statement and no where condition, so all rows will be fetched.

Here customerid specifies to only customerid column ,which will be

fetched from customers table. No where condition so all rows will

be fetched.

Here in this example WHERE condition filter the data as the

condition is the customerid and city with the customerid=2 should

be taken out.

Page 7: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Customer belongs to city “Chicago” should be filtered.

Sample Output:

And All data in the customers table is taken out. - Query and it’s

result.

And Only customerid from the table is taken out - Query and it’s

result.

And only customerid=2 is taken out - Query and it’s result

And only customer belong to city =’chicago’ is taken out - Query

and it’s result.

Page 8: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

Example - Using TOP keyword

Below is the syntax for Top Keyword in select query

Syntax:

This SQL Server SELECT provides the number of records based on the

top value specified in the query.

Example - Using TOP PERCENT keyword

Let's look at a SQL Server example, where we use the TOP PERCENT

keyword in the SELECT statement.

Syntax for Top percent

SELECT TOP(value)

Column_Name1, column_name2

FROM table_name

[WHERE condition]

[ORDER BY column_name asc/desc]

SELECT TOP(value) percent

Column_Name1, column_name2

FROM table_name

[WHERE condition]

[ORDER BY column_name asc/desc]

Page 9: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

This SQL Server SELECT statement will provide the percentage of

records based on the value provided in the above syntax.

Sample sql Queries:

select * from dbo.Customers

-- fetch all the records from the customers table

select Top(2) * from dbo.Customers

-- fetch top 2 records from the customers table

select Top(60) percent * from dbo.Customers

– fetch top 60 percentage of records from customers table.

Code Explanation:

All records from the table dbo.Customers will be shown.

Page 10: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

2 records from the table dbo.Customers will be executed by the

select statement. By default it will take the order of records available

in the table for fetching.

60% of the total records will be fetched and given as result set to the

user.

Sample Output:

Page 11: Select query in SQL Server - Wikitechy · The SQL Server SELECT statement is used to retrieve data from one or more tables in SQL Server database. Retrieving data from multiple tables

|SQL-Server

Facebook.com/wikitechy twitter.com/WikitechyCom © Copyright 2016. All Rights Reserved.

And 5 records available in the table. Query and it’s result

And Top 2 records fetched and given. Query and it’s result

And 60% of 5 record is 3. 3 rows returned. Query and it’s result

set.