4. Where clause
WHERE Clause: used to filter records (extract only those records that fulfill a specified condition).
Syntax:
SELECT column1, column2, column3
FROM table_name
WHERE [condition]
Example:
Write a query using a WHERE clause that displays all the employees listed in the DimEmployee table
who have the job title “Research and Development Engineer”. Display the EmployeeKey, the login ID,
and the title for each one.
SELECT EmployeeKey, LoginID, Title
FROM DimEmployee
WHERE Title = 'Research and Development Engineer'
5. Operators with WHERE
Group Operator Description
Comparison operators
= Equal
<> Or != Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
Logical operators
AND Return records that meet all the conditions separated by AND in WHERE clause.
OR
Return records that meet any of the conditions separated by OR in WHERE
clause.
NOT Return records that do not satisfy any of the conditions in WHERE clause.
SQL operators
[NOT] BETWEEN Returns values [NOT] within a given range
[NOT] IN
Specify multiple values in a WHERE clause (a shorthand for multiple OR
conditions)
IS [NOT] NULL Returns records having [NOT] NULL values in the given fileds.
[NOT] LIKE Returns records that [DO NOT] match a specified pattern in a column.
6. SQL operators
BETWEEN Syntax (https://docs.microsoft.com/en-us/sql/t-sql/language-elements/between-transact-sql?view=sql-
server-ver15)
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
IN Syntax (https://docs.microsoft.com/en-us/sql/t-sql/language-elements/in-transact-sql?view=sql-server-ver15)
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
7. SQL operators
LIKE Syntax: https://docs.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql?view=sql-server-ver15
SELECT column1, column2, ...
FROM table_name
WHERE column N LIKE pattern;
Wildcard character Description Example
% Any string of zero or more characters. WHERE title LIKE '%computer%' finds all book titles with the word 'computer'
anywhere in the book title.
_ (underscore) Any single character. WHERE fname LIKE '_ean' finds all four-letter first names that end with ean
(Dean, Sean, and so on).
[ ] Any single character within the specified range ([a-f])
or set ([abcdef]).
WHERE lname LIKE '[C-P]arsen' finds author last names ending with arsen
and starting with any single character between C and P, for example Carsen,
Larsen, Karsen, and so on. In range searches, the characters included in the
range may vary depending on the sorting rules of the collation.
[^] Any single character not within the specified range
([^a-f]) or set ([^abcdef]).
WHERE name LIKE 'de[^l]%' finds all author last names starting with de and
where
8. Where clause – Exercise
Exercise: Use AdventureWorksDW2019
From table “DimEmployee” select all records that satisfy one of the following conditions:
• DepartmentName is equal to “Tool Design”
• Status does NOT include the value NULL
• StartDate in the period from ‘2009-01-01’ to ‘2009-12-31’
And must have VacationHours >10
10. Data Types- String types
Data type Description Max size
char(n) Fixed width character string 8,000 characters
varchar(n) Variable width character string 8,000 characters
varchar(max) Variable width character string 1,073,741,824 characters
text Variable width character string 2GB of text data
nchar(n) Fixed width Unicode string 4,000 characters
nvarchar(n) Variable width Unicode string 4,000 characters
nvarchar(max) Variable width Unicode string 536,870,912 characters
ntext Variable width Unicode string 2GB of text data
binary(n) Fixed width binary string 8,000 bytes
Varbinary(n) Variable width binary string 8,000 bytes
varbinary(max) Variable width binary string 2GB
image Variable width binary string 2GB
11. Data Types- Number types
Data type Description Max size
bit Integer that can be 0, 1, or NULL
tinyint Allows whole numbers from 0 to 255 1 byte
smallint Allows whole numbers between -215
and 215
2 bytes
int Allows whole numbers between -231
and 231
4 bytes
bigint Allows whole numbers between -263
and 263
8 bytes
decimal (p,s)
Fixed precision and scale numbers.
Allows numbers from -1038
+1 to 1038
-1.
5-17 bytes
numeric(p,s)
Fixed precision and scale numbers.
Allows numbers from -1038
+1 to 1038
-1.
5-17 bytes
money Monetary data from -263
to 263
8 bytes
smallmoney Monetary data from -214,748.3648 to 214,748.3647 4 bytes
float(n) Floating precision number data from -1.79E + 308 to 1.79E + 308. 4 or 8 bytes
real Floating precision number data from -3.40E + 38 to 3.40E + 38 4 bytes
12. Data Types- Datetime types
Data type Description Max size
datetime From 1/1/1753 to 31/12/9999 with an accuracy of 3.33 milliseconds 8 bytes
datetime2 From 1/1/0001 to 31/12/9999 with an accuracy of 100 nanoseconds 6-8 bytes
smalldatetime From 1/1/1900 to 06/06/2079 with an accuracy of 1 minute 4 bytes
date Store a date only. From 1/1/0001 to 31/12/9999 3 bytes
time Store a time only to an accuracy of 100 nanoseconds 3-5 bytes
datetimeoffset The same as datetime2 with the addition of a time zone offset 8-10 bytes
13. NULL Values
NULL represents a missing or unknown value
ANSI behaviour for NULL values:
• The result of any expression containing a NULL value is NULL
2 + NULL = NULL
'MyString: ' + NULL = NULL
• Equality comparisons (=) always return false for NULL values, use IS NULL
NULL = NULL returns false
NULL IS NULL returns true
Useful functions:
ISNULL(column/variable, value): Returns value if the column or variable is NULL
* COALESCE(column/variable1, column/variable2, ...): Returns the value of the first non-NULL column or variable
in the list
14. Conversion function
• CAST() converts a value (of any type) into a specified datatype
-- CAST(expression AS data_type[length])
• CONVERT() converts a value (of any type) into a specified datatype
-- CONVERT(datatype(length), expression, style)
(More Date and Time style: https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-
server/)
SELECT CAST('2022' as int) as new_datatype
SELECT CONVERT(int, '2022') as new_datatype
SELECT CONVERT(VARCHAR,GETDATE(),21) /* 22? */
• Compatible data types can be implicitly converted
• Explicit conversion requires an explicit conversion function:
CAST / TRY_CAST
CONVERT / TRY_CONVERT
STR
16. Date Function
• DAY() returns the day of the month for a given date
-- DAY(date) --
SELECT DAY('2022/03/10') AS get_day
• MONTH() returns the month part for a given date (a number from 1 to 12)
-- MONTH(date) --
SELECT MONTH'2022/03/10') AS get_month
• YEAR() returns the year part for a given date
-- YEAR(date) --
SELECT YEAR('2022/03/10') AS get_year
• DATEPART() returns a specified part of a date (as integer)
-- DATEPART(interval, date) --
SELECT DATEPART(year, '2022/03/10') AS date_part_int