Advanced SQL
Advanced SQL
Operator
Comparison operators in the
Description
WHERE clause
Example
= Equal to WHERE salary = 50000
!= or <> Not equal to WHERE department <> 'HR'
> Greater than WHERE age > 30
< Less than WHERE salary < 40000
>= Greater than or equal to WHERE experience >= 5
IS NOT NULL Checks for NOT NULL values WHERE manager_id IS NOT NULL
Con..
What is an Index?
An index in SQL is a performance optimization tool
used to speed up the retrieval of rows from a table by
providing quick access to the data.
Think of it like the index of a book — it helps locate
information faster without scanning every page.
Basic syntax:-
CREATE INDEX index_name
ON table_name (column1, column2, ...);
Example 1: Single Column Index
CREATE INDEX idx_lastname
ON employees (last_name);
Con..
Example 2: Multi-Column (Composite) Index
CREATE INDEX idx_name_dept
ON employees (last_name, department);
Useful when queries filter using both last_name and
department.
When to Use Indexes:
Primary and foreign key columns
Columns used in WHERE, JOIN, ORDER BY
Avoid on:
Small tables
Columns with many duplicate values
Columns rarely used in filtering or sorting
DROP INDEX idx_lastname;
Con..
Feature Benefit
Function Description
Arranges the output rows in a specific
1. Sorting Results
order.
Makes reports and outputs easier to
2. Improves Data Readability
analyze.
Useful when sorting based on primary and
3. Can Sort by Multiple Columns
secondary fields.
4. Works with Numeric, Text, and Date
Supports sorting on all data types.
Columns
5. Default is Ascending (ASC) Ascending order if not explicitly stated.
Con..
Syntax:-
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2
[ASC|DESC];
Sort by One Column (Ascending - Default):
SELECT * FROM employees ORDER BY name;
Sort by One Column (Descending):
SELECT * FROM employees ORDER BY salary DESC;
Con..
Sort by Multiple Columns:
SELECT * FROM employees ORDER BY
department ASC, salary DESC;
generally
Keyword Function
ALL TRUE if all of the sub query values meet the condition
ANY TRUE if any of the sub query values meet the condition
Returns TRUE if ALL of the sub query values meet the condition
Name
Tilahun
Chemdesa
Con..
Duplicated values
Duplicates can be a big problem in SQL
databases as they can slow down query
performance and waste valuable storage space.
When the result set from a SELECT statement
contains duplicate rows, you may want to
remove them and keep every row data to be
unique for a column or combination of columns.
You can use the DISTINCT identifier to
eliminate duplicate records from the result set.
Con..
Consider the following facts when using
DISTINCT identifier in a SELECT statement:
In a SELECT statement, include DISTINCT
keyword after the SELECT clause.
Multiple columns can be specified after DISTINCT
keyword. In this case, the result set contains distinct
combination of data from these columns.
DISTINCT keyword syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
Null Value
Rollback to B;
Rollback to A;
Arithmetic operations
String functions and operators
Mathematical functions
Date functions
2.1.Arithmetic operations
Function Description
ASCII Returns the ASCII for the specified character
CHAR Returns the character based on ASCII
CHARINDEX Returns the position of a substring in a string
DATALENGTH Returns the number of bytes used to represent an expression
ASCII (character)
Character parameter returns the ASCII value. If
more than one character is entered, it will only
return the value for the first character
Con..
The CHAR () function returns the character based on
the ASCII code.
CHAR syntax
CHAR (code)
Code parameter returns the ASCII number code for the character
The CHARINDEX () function searches for a substring in a string, and
returns the position.
If the substring is not found, this function returns 0.
Con..
CHARINDEX syntax
CHARINDEX (substring, string, start)
Substring parameter is the one to search for
DATALENGTH syntax
DATALENGTH (expression)
Expression parameter is the one that return data type length
Con..
DIFFERENCE syntax
UPPER (text)
Con..
Text parameter is the string to be converted
LTRIM syntax
LTRIM (string)
String parameter is the one to remove leading spaces from.
Con..
COUNT () Syntax
COUNT (expression)
Expression parameter is a field or a string value
Con..
The AVG () function returns the average value of an expression.
AVG () syntax
AVG (expression), the Expression parameter is a numeric value
The DEGREES () function converts a value in radians to degrees.
DEGREES () syntax
DEGREES (number)
Number parameter is a numeric value
The MAX () function returns the maximum value in a set of values.
MAX () syntax
MAX (expression)
Con..
Expression parameter is a numeric value
SQUARE (number)
Number parameter is a positive number to calculate the square
SUM () syntax
SUM (expression)
Con..
Expression parameter is a field or a formula
POWER (a, b)
•A parameter is a number (the base) and B parameter is a number (the
exponent)
TAN (number)
Con..
COS (number)
Number parameter is a numeric value
Con..
CEILING (number)
Number parameter is a numeric value
2.4. Date functions
DATEADD Adds a time/date interval to a date and then returns the date
CURRENT_TIMESTAMP
The DATEADD () function adds or subtracts a
time/date interval to a date and then returns the date.
DATEADD () syntax
Con..
yyyy, yy = Year
qq, q = Quarter
mm, m = month
dy, y = Day of the year
dd, d = Day
ww, wk = Week
dw, w = Weekday
hh = hour
mi, n = Minute
ss, s = Second
ms = Millisecond
start date and end date parameters are the two dates to calculate the difference
between.
Con..
DATEPART (datepart,date)
Where date is a valid date expression and date part
can be one of the following:
yyyy, yy = Year
qq, q = Quarter
mm, m = month
dy, y = Day of the year
dd, d = Day
ww, wk = Week
dw, w = Weekday
hh = hour
mi, n = Minute
ss, s = Second
ms = Millisecond
Con..
The CONVERT () function is a general function that converts an
expression of one data type to another.
The CONVERT () function can be used to display date/time data in
different formats.
CONVERT () syntax
CONVERT (data_type(length),expression,style)
data_type(length) Specifies the target data type (with an optional
length)
expression Specifies the value to be converted
style Specifies the output format for the date/time
Con..
GETDATE ()
Unit Three: SQL statements with aggregation and filtering
Features
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Con..
Syntax