select * from tblEmployee
select * from tblEmployee
where age = 30
select * from tblEmployee
where age = 30
where age
----> select records and filter based on the age column = 30
----> is equal to 30
Diagram 1
where
clause uses conditional operators to filter records.where
clause is always followed by a subject which is followed by an operator. select * from tblEmployee
where age > 18
>
----> is greater (conditional operator)
Diagram 2
SELECT * FROM tblEmployee
WHERE FullName LIKE '%r'
SELECT * FROM tblEmployee
WHERE age = 10 and salary =10000
SELECT * FROM tblEmployee
WHERE age = 10 and salary =10000
and
--> If both the first condition (age = 10
) AND the second condition (salary =10000
) that you provided are true then one or many records gets displayed.SELECT * FROM tblEmployee
WHERE age between 10 and 15
SELECT * FROM tblEmployee
WHERE age between 10 and 15
between 10 and 15
----> range specified is 10 to 15 SELECT * FROM tblEmployee
WHERE age = 10 OR salary =10000
SELECT * FROM tblEmployee
WHERE age = 10 OR salary =10000
OR
----> If either the first OR the Second condition that you provided is true then one or many records gets displayed. 3. Execute the query.Select FullName,age
from tblEmployee
Select FullName,age
----> After the select keyword Individual columns names are specified followed by commas , when selecting individual columns SELECT FullName as Employee_Full_Name
FROM tblEmployee
SELECT FullName as Employee_Full_Name
FROM tblEmployee
as Employee_Full_Name
----> A more readable temporary column nameas
keyword as is always followed by a temporary name without spaces. select * from tblEmployee
order by age asc
select * from tblEmployee
order by age asc
order by age asc
----> sort the records by age starting the youngest employee. To select top 3 most paid employees
select top 3 * from tblEmployee
order by salary desc
select top 3 * from tblEmployee
order by salary desc
select top 3 *
----> Select the top 3 records that the query returned from the table.SELECT TOP
clause can be used to specify the number of records to return from a table.
AVG() - Returns the average value
Select avg(Salary) AS Average_Salary
from tblEmployee
avg(column_name)
----> returns the average value of a the column name you specify. avg(Salary) AS Average_Salary
----> You need to alias the column name to ensure that a column name is displayed along with its values.
COUNT() - Returns the number of rows
Select Count(fullName) as Number_Of_Employee
from tblEmployee
Count(fullName)
----> Returns the number of number of values the fullName column has.SUM() - Returns the sum
Select sum(salary) as Total
tblEmployee
count()
function the sum()
function requires the column those name has been provided to contain digits. Challenge:
Try using the following aggregate functions on your own. Also feel free to post your result in the comment section of this post.
MAX()
- Returns the largest value of a specified column.MIN()
- Returns the smallest value of a specified column.
Lower () - Converts a field to upper case
Lower(fullName) as Lower_Case_Names
tblEmployee
Lower(fullName)
----> Change all text values (fields) from the "fullName" column to lowercase
LEN() - Provides the length of a text field that you specify
'Joe'
----> a set of characters (string
) , also a parameter for the Len()
function. select LEN(fullName) AS Name_Length
from tblEmployee
LEN(fullName)
----> Returns the length of all the fields of the fullName columnChallenge:
Try using the following scalar functions on your own. Also feel free to post your result in the comment section of this post.
Upper()
- Converts a field to upper case Download link: T-SQL_Intermediate_Tutorial.docx |