SQL Day 10: Select Statement – Part 1
LO1: Select Specific or All Columns
LO2: Distinct Rows
LO3: Filtering with Where Clause
Prerequisite: SQL Day 3: Creating Tables & Enforcing Primary and Foreign Key
The Select statement is used to retrieve the all columns or specific columns from a table.
Different Methods to Retrieve the Columns Using Select
1. Using * Sign
When we use * sign is select statement then this will retrieve all the columns list of a table.
Syntax:
select * from (Table_Name)
Example:
select * from tblEmployee
2. Using all Column List
Same as the * sign but can retrieve the selected columns as per the given list of columns.
Syntax:
select (Column_Names) from (Table_Name)
Example:
select ID, Name, Email from tblEmployee
3. Writing Select Query Graphically
We can also generate the select statement graphically. To do that, you need to right-click on the table in Object Explorer and then go to the New Query Editor Window as per the below screenshot.
This will create the select statement for the selected table.
In this table, you can see that table is mentioned as three part table name. This is the fully qualified name of the table.
[Sample].[dbo].[tblEmployee]
The [Sample] is the database name, [dbo] is the schema (This will be discussed in later sessions) and [tblEmployee] is the table name.
When we are in the different context of the database and execute the query then the query will get executed successfully.
Selecting Distinct Rows
Distinct is used to remove the duplicate values from the columns i.e. if we are using the Distinct keyword with the select statement then we will get the unique values.
Let’s say we have the table tblEmployee in which City ‘London’ is repeated twice.
Now if we want to select the unique city then we can use the Distinct keyword in the select statement.
Syntax:
select Distinct (Column_Names) from (Table_Name)
Example:
select Distinct City from tblEmployee
You can see that only 6 rows are returned by running above query because of the Distinct keyword.
If we use the Distinct keyword with multiple columns then the values should be distinct across these columns.
For example, if we take Name with City column then all the 7 rows will return because the values are not distinct in City and the Name column.
select Distinct Name, City from tblEmployee
Filtering with Where Clause
Where clause is used to filter the rows on the specified condition.
Let’s say we want all the people names who lived in London, to do that we need to add a where clause in the select statement.
Syntax:
select * from (Table_Name) where (Condition)
Example:
select * from tblEmployee where City = 'London'
Now, Let’s say we want all the people names who not lived in London, to do that we need to add a where clause in the select statement.
select * from tblEmployee where City != 'London'
or
select * from tblEmployee where City <> 'London'
Operator != and <> is considered as NOT EQUAL to operator.
There are many operators that we used with where clause like >, <, >=, <=, BETWEEN, IN, LIKE and NOT.
We also have some wild card Operators %, _, [] and [^].
These operators will be discussed in next part.
That’s all for today. Thank You.
Comments
Post a Comment