2. LECTURE CONTENTS
1. IN Operator
2. BETWEEN Operator
3. NOT BETWEEN Operator
4. SQL Aliases
5. Usefulness of SQL Aliases
Database Systems 2
3. IN Operator
Database Systems 3
• The IN operator allows you to specify multiple values in a WHERE clause.
IN Operator Example
• The following SQL statement selects all customers with a City of "Paris" or
"London":
SELECT *
FROM Customers
WHERE City IN ('Paris','London');
4. IN Operator
• The IN operator is a logical operator used in SQL (Structured Query
Language) to determine whether a specified value matches any value
in a list. It is commonly used in the WHERE clause of a SQL statement
to filter rows based on a set of predefined values.
Here's how the IN operator works:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Database Systems 4
5. For example
• For example, suppose you have a table called products with a column
named category. You can use the IN operator to select all products
that belong to either the 'Electronics' or 'Clothing' categories:
SELECT *
• FROM products
• WHERE category IN ('Electronics', 'Clothing');
Database Systems 5
6. BETWEEN Operator
Database Systems 6
• The BETWEEN operator selects values within a range.
• The values can be numbers, text, or dates.
Example
• The following SQL statement selects all products with a price BETWEEN 10 and 20:
SELECT *
FROM Products
WHERE Price BETWEEN 10 AND 20;
7. BETWEEN operator
• In SQL, the BETWEEN operator is used to filter data based on a range
of values. It's typically used in the WHERE clause of a SQL query to
select rows where a column value falls within a specified range.
• Here's the basic syntax for using the BETWEEN operator:
• SELECT column_name(s)
• FROM table_name
• WHERE column_name BETWEEN value1 AND value2;
Database Systems 7
8. For example
• For example, suppose you have a table called employees with a
column named salary. You can use the BETWEEN operator to select all
employees whose salary falls within a specific range:
• SELECT *
• FROM employees
• WHERE salary BETWEEN 40000 AND 60000;
• This query will return all rows from the employees table where the
salary column value is between $40,000 and $60,000, inclusive.
Database Systems 8
9. NOT BETWEEN Operator
Database Systems 9
• To display the products outside the range of the previous example, use
NOT BETWEEN:
Example
SELECT *
FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
10. • An example of a situation where the concept of "not between" might be
applicable:
Let's say you have a dataset containing information about the ages of participants
in a study. You want to filter out the individuals who are not between the ages of
18 and 30. In SQL, you might write a query like this:
SELECT *
• FROM participants
• WHERE age NOT BETWEEN 18 AND 30;
• This query would retrieve all records from the "participants" table where the age
is
• not between 18 and 30, meaning it would return individuals
• who are younger than 18 or older than 30.
Database Systems 10
11. BETWEEN Operator with IN Example
Database Systems 11
• The following SQL statement selects all products with a price BETWEEN 10 and 20,
but products with a CategoryID of 1, 2 or 3 should not be displayed:
Example:
SELECT *
FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3);
12. BETWEEN Operator with IN Example
• SELECT *
• FROM products
• WHERE price BETWEEN 10 AND 50
• AND category IN ('Electronics', 'Clothing', 'Home Goods');
• In this query:
The BETWEEN operator filters products with prices between $10 and
$50.
• The IN operator filters products that belong to categories such as
'Electronics', 'Clothing', or 'Home Goods'.
Database Systems 12
13. BETWEEN Operator with Text Value
Database Systems 13
• The following SQL statement selects all products with a ProductName beginning
with any of the letter BETWEEN 'C' and 'M':
SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M';
14. BETWEEN with text values
• When using BETWEEN with text values, it operates based on their
alphabetical order, rather than any inherent numeric or chronological
sequence. For instance, suppose you have a dataset of products with
names, and you want to select products whose names fall within a
certain alphabetical range. Here's an example:
SELECT *
• FROM products
• WHERE product_name BETWEEN 'A' AND 'F';
Database Systems 14
15. • This query would retrieve all records from the "products" table where
the product_name falls between 'A' and 'F' alphabetically.
• It would include products with names like 'Apple', 'Banana', 'Carrot',
but exclude products with names starting with letters after 'F', such as
'Grapes', 'Lemon', etc.
Database Systems 15
16. NOT BETWEEN Operator with Text Value Example
Database Systems 16
• The following SQL statement selects all products with a ProductName beginning
with any of the letter NOT BETWEEN 'C' and 'M':
SELECT * FROM Products
WHERE ProductName NOT BETWEEN 'C' AND 'M';
17. BETWEEN Operator with Date Value
Database Systems 17
• The following SQL statement selects all orders with an OrderDate
BETWEEN '04-July-1996' and '09-July-1996':
SELECT *
FROM Orders
WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#;
18. BETWEEN Operator with Date Value
• Suppose you have a table named "orders" containing information
about orders placed by customers, including the order date. You want
to retrieve orders placed between January 1, 2023, and December 31,
2023. You can use the BETWEEN operator with date values in SQL like
this:
SELECT *
• FROM orders
• WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';
Database Systems 18
19. task
• Using the IN operator:
• "The book is either in the fiction section or in the mystery section."
• Using the BETWEEN operator:
• "The temperature is between 70 and 80 degrees Fahrenheit."
• Using the NOT BETWEEN operator:
• "The deadline for submissions is not between January 1st and January
15th."
Database Systems 19
20. SQL Aliases
Database Systems 20
• SQL aliases are used to temporarily rename a table or a column heading
• SQL aliases are used to give a database table, or a column in a table, a temporary
name
• Basically aliases are created to make column names more readable
SQLAlias Syntax for Columns
SELECT column_name AS alias_name
FROM table_name;
SQLAlias Syntax for Tables
SELECT column_name(s)
FROM table_name AS alias_name;
21. Aliases
• Aliases in the context of databases or SQL (Structured Query
Language) are temporary names assigned to columns or tables in a
SQL statement. They are used to make column or table names more
readable or to provide a shorter name for convenience.
• For example, in a SQL query, you might have a table named
"employees" with columns like "employee_id", "first_name", and
"last_name". Instead of typing out the full table and column names
every time you reference them in a query, you can assign aliases to
them.
Database Systems 21
22. Alias Example for Table Columns
Database Systems 22
• The following SQL statement specifies two aliases, one for the CustomerName column and one
for the ContactName column.
• Tip: It require double quotation marks or square brackets if the column name contains spaces:
SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;
• In the following SQL statement we combine four columns (Address, City, PostalCode, and
Country) and create an alias named "Address":
SELECT CustomerName, Address+', '+City+', '+PostalCode+', '+Country AS Address
FROM Customers;
23. Alias Example for Table
Database Systems 23
• The following SQL statement selects all the orders from the customer with
CustomerName=(Around the Horn). We use the "Customers" and "Orders" tables, and
give them the table aliases of "c" and "o" respectively (Here we have used aliases to
make the SQL shorter):
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName="Around the Horn" AND
c.CustomerID=o.CustomerID;
• The same SQL statement without aliases:
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName="Around the Horn" AND
Customers.CustomerID=Orders.CustomerID;
24. Aliases can be useful when:
Database Systems 24
• There are more than one table involved in a query
• Functions are used in the query
• Column names are big or not very readable
• Two or more columns are combined together
25. LECTURE CONTENTS
1. SQL AVG( ) Function
2. COUNT() Function
3. MAX( ) Function
4. MIN( ) Function
5. SUM( ) Function
6. UCASE( ) Function
7. MID( ) Function
Database Systems 25
26. AVG( ) Function
Database Systems 26
• The following SQL statement gets the average value of the "Price" column from the
"Products" table:
Example
SELECT AVG(Price) AS PriceAverage
FROM Products;
27. AVG() Function (Contd…)
Database Systems 27
The following SQL statement selects the "ProductName" and "Price" records that have
an above average price:
Example
SELECT ProductName, Price
FROM Products
WHERE Price>(SELECT AVG(Price) FROM Products);
28. • The AVG() function in SQL is used to calculate the average value of a
set of values in a column. It is often used in conjunction with the
SELECT statement to retrieve the average value of a specific column
or as part of a larger query. Here's how you can use the AVG()
function:
• Syntax:
• SELECT AVG(column_name)
• FROM table_name;
Database Systems 28
29. • Example:
• Suppose you have a table named "grades" with a column named
"score" representing the scores of students in a class. You want to
calculate the average score of all students. Here's how you would use
the AVG() function:
• SELECT AVG(score) AS average_score
• FROM grades;
Database Systems 29
30. • This query will calculate the average value of the "score" column in the "grades"
table and return it as a single result. The AS average_score part of the query
assigns a label "average_score" to the calculated average value in the result set.
• You can also use the AVG() function with a GROUP BY clause to calculate average
values for different groups within your data. For example, if you wanted to
calculate the average score for each subject in your class, you could do:
• SELECT subject, AVG(score) AS average_score
• FROM grades
• GROUP BY subject;
Database Systems 30
31. TASK
• Here's a SQL question that involves the usage of IN, BETWEEN, NOT BETWEEN, SUM, COUNT, and AVG:
• Consider a table named sales_data with the following schema:
• product_id (integer): The ID of the product.
• sales_amount (numeric): The amount of sales for the product.
• sales_date (date): The date of the sales transaction.
• Write a SQL query to find the total sales amount, the count of products sold, and the average sales amount for products sold between January 1,
2024, and January 31, 2024, for products with IDs 1, 2, and 3.
• Your query should:
• Select the total sales amount, the count of products sold, and the average sales amount.
• Filter the sales data for products with IDs 1, 2, and 3 using the IN clause.
• Filter the sales data for transactions that occurred between January 1, 2024, and January 31, 2024, using the BETWEEN clause.
• Ensure that the date range does not include January 31, 2024, using the NOT BETWEEN clause.
Database Systems 31
32. COUNT() Function
Database Systems 32
• The COUNT() function returns the number of rows that matches a specified criteria
• The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*)
FROM table_name;
SQL COUNT(DISTINCT column_name) Syntax
• The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified
column:
SELECT COUNT(DISTINCT column_name)
FROM table_name;
33. • The COUNT() function is a commonly used function in database
management systems (DBMS) such as SQL. It is used to count the
number of rows that meet a specified condition within a table.
• In its simplest form, the COUNT() function returns the total number of
rows in a specified table. For example:
SELECT COUNT(*) FROM tableName;
Database Systems 33
34. • This query will return the total number of rows in the "tableName"
table.
• The COUNT() function can also be used with a specific column to
count the number of non-null values in that column. For example:
SELECT COUNT(columnName) FROM tableName;
Database Systems 34
35. • This query will return the number of non-null values in the
"columnName" column of the "tableName" table.
• Additionally, the COUNT() function can be combined with other SQL
clauses, such as WHERE, GROUP BY, HAVING, etc., to count rows
based on specific conditions or groupings. For example:
SELECT COUNT(*) FROM tableName WHERE condition;
Database Systems 35
36. COUNT() Function (Contd…)
Database Systems
SQL COUNT(DISTINCT column_name)
Example
• The following SQL statement counts the number of unique customers in the "Orders"
table:
SELECT COUNT(DISTINCT CustomerID) AS NumberOfCustomers
FROM Orders;
• The function COUNT (*) counts all rows selected by a query regardless of whether
any of the rows contain null values
• COUNT calculates only rows that contain values; it ignores all null values
37. MAX( ) Function
Database Systems 37
• The MAX() function returns the largest value of the selected column
• The following SQL statement gets the largest value of the "Price" column
from the "Products" table:
Example
SELECT MAX(Price) As HighestPrice
FROM Products;
38. MIN( ) Function
Database Systems 38
• The MIN( ) function returns the smallest value of the selected column.
SQL MIN( ) Example
• The following SQL statement gets the smallest value of the "Price"
column from the "Products" table:
Example
SELECT MIN(Price) AS SmallestOrderPrice
FROM Products;
39. SUM( ) Function
Database Systems 39
• The SUM() function returns the total sum of a numeric column.
SQL SUM( ) Example
• The following SQL statement finds the sum of all the "Quantity" fields for
the "OrderDetails" table:
Example
SELECT SUM(Quantity) AS Total_Items_Ordered
FROM OrderDetails;
40. • The SUM() function is another common aggregate function in SQL
used to calculate the sum of values in a specified column. Here are
some examples of how the SUM() function can be used:
• Basic usage to find the total sum of a column:
• sql
• Copy code
• SELECT SUM(columnName) FROM tableName;
Database Systems 40
41. • This query will return the sum of all the values in the "columnName"
column of the "tableName" table.
• Using the SUM() function with a WHERE clause to sum values based
on a condition:
• sql
• Copy code
• SELECT SUM(columnName) FROM tableName WHERE condition;
• This query will return the sum of values in the "columnName" column
of the "tableName" table that satisfy the specified condition.
Database Systems 41
42. • Combining the SUM() function with other columns and calculations:
• SELECT SUM(column1 + column2) FROM tableName;
• This query will return the sum of the values obtained by adding
corresponding values from "column1" and "column2" in the "tableName"
table.
• Using the SUM() function with GROUP BY clause to calculate the sum for
each group:
• SELECT groupingColumn, SUM(columnName) FROM tableName GROUP BY
groupingColumn;
• This query will return the sum of values in the "columnName" column for
each distinct value in the "groupingColumn" column.
Database Systems 42
43. task
• "In the sales department, the average monthly revenue generated
from product X is $10,000, with a total count of 12 months. The
maximum monthly revenue reached $15,000, while the minimum
was $7,000. The total revenue for the year sums up to $120,000."
Database Systems 43
44. UCASE( ) Function
Database Systems 44
• The UCASE() function converts the value of a field to uppercase
SQL UCASE( ) Example
• The following SQL statement selects the "CustomerName" and "City"
columns from the "Customers" table, and converts the "CustomerName"
column to uppercase:
• Similar is Lower CASE, use LCASE
Example
• SELECT UCASE(CustomerName) AS Customer
FROM Customers;
45. • the UPPER() function in SQL is used to convert a string to uppercase. Here are
some examples of how to use it:
• Basic usage to convert a string to uppercase:
• SELECT UPPER('hello') AS uppercase_string;
• This query will return 'HELLO' as the result.
• Using the UPPER() function with a column name to convert the values in that
column to uppercase:
• SELECT UPPER(columnName) AS uppercase_column FROM tableName;
• This query will return the values in the "columnName" column of the
"tableName" table, but all in uppercase.
Database Systems 45
46. • Combining the UPPER() function with other SQL functions or clauses:
• SELECT UPPER(CONCAT(first_name, ' ', last_name)) AS full_name
FROM employees;
• This query will concatenate the "first_name" and "last_name"
columns from the "employees" table and convert the result to
uppercase.
• Using the UPPER() function in a WHERE clause:
• SELECT * FROM tableName WHERE UPPER(columnName) =
'SEARCHSTRING';
Database Systems 46
47. MID( ) Function
Database Systems 47
• The MID( ) function is used to extract characters from a text field
SQL MID( ) Example
• The following SQL statement selects the first four characters from the
"City" column from the "Customers" table:
Example
• SELECT MID(City,1,4) AS ShortCity
FROM Customers;
48. Task
• Suppose we have a table named employee_details with the following
schema:
• employee_id (integer): The unique ID of the employee.
• first_name (string): The first name of the employee.
• last_name (string): The last name of the employee.
• salary (numeric): The salary of the employee.
• department (string): The department in which the employee works.
• Write a SQL query to find the following:
Database Systems 48
49. • The total count of employees.
• The minimum and maximum salaries among all employees.
• The total sum of salaries for all employees.
• The uppercase version of the first name of the employee whose last name starts
with 'S'.
• The middle three characters of the first name for employees whose last name
starts with 'A'.
• Your query should:
• Utilize the COUNT, MIN, MAX, SUM, UPPER, and MID functions.
• Provide meaningful aliases for the calculated values.
• Include necessary conditions to filter data according to the given requirements.
Database Systems 49