Advanced Database Systems
Advanced Database Systems
DATABASE
SYSTEMS
DATABASE CONCEPT
Database
• It is simply a collection of structured data that is organized and stored in a
way that allows easy access, management, and updating. It typically consists of:
o Tables (for relational databases) or other structures (for NoSQL databases
like collections, documents, etc.).
o Data stored in these tables or structures (e.g., rows in a table).
o Types: Relational, NoSQL, Object-oriented, Data Warehouses, Distributed
databases, Document database
o Examples: Relational – MySQL, Microsoft SQL Server, Oracle Database;
NoSQL – MongoDB, Cassandra, Couchbase
• It is managed using specialized software called a Database Management
System (DBMS), which allows users to store, retrieve, and manipulate data
efficiently.
• Its primary role is to store data efficiently and securely.
3
DATABASE CONCEPT
Database Management System (DBMS)
• It is software that helps you interact with the database to perform
operations such as creating, reading, updating, and deleting data.
• It includes a database engine, user interface, and data manipulation
languages.
• It handles tasks like data storage, backup and recovery, security (control
access to the data, user permissions), and data integrity (ensure
consistency)
• Perform queries (via SQL in relational DBMS or other query languages
for NoSQL).
4
DATABASE CONCEPT
Relational Database
• A relational database is a type of database that organizes data into rows
and columns, which collectively form a table where the data points are
related to each other.
• Data is typically structured across multiple tables, which can be joined
together via a primary key or a foreign key.
Table
• It is a collection of related data entries and it consists of columns and
rows.
Record or Row
• It an an individual entry that exists in a table
5
DATABASE CONCEPT
6
DATABASE CONCEPT
Column
• It represents a set of data values of a particular type, one for each row of
the table. For instance, the Student Name column would contain the
names of all students in the Students table.
Field
• It is frequently used in the context of a single record;
• It is the intersection of a row and a column in a table. It contains the
actual value for a specific attribute of a record.
• For instance, in a Student record, the Student Name field could contain
the value “John Doe”.
7
DATABASE CONCEPT
Attribute
• In an Entity-Relationship Diagram (ERD), an attribute is a characteristic or
property that defines an entity in the model. When an ERD is translated
into a physical database model, entities become tables, and attributes
become columns.
8
DATABASE CONCEPT
Attribute
• Within the context of a database, the term attribute can mean any property
that a database object (table, column, schema, trigger, stored procedure,
view, etc.) can possess.
• However, it is most often used to discuss the properties of a table’s column.
1. Name: Every column must have a unique name within its table.
2. Data Type: This defines the kind of data the column can store. It can
be integers, floating-point numbers, strings, dates, Boolean values, or
other types of data depending on the database system.
3. Length or Size: For some data types, such as strings (VARCHAR,
CHAR), you can specify the maximum length.
4. Default Value: You can specify a default value the database system uses
if no value is explicitly provided when a row is inserted.
9
DATABASE CONCEPT
Expressions
• Are combinations of symbols, operators, values, and functions used to perform
calculations, manipulate data, or evaluate conditions. They are a fundamental
part of querying and working with data in a database..
Null Value
• A null value represents the absence of any value. It means that the data is
unknown or missing. It can be assigned to fields of any data type, including
strings, integers, and dates. Importantly, the field is not allocated memory since
NULL signifies an unknown value.
Blank (Empty) Value
• It refers to an empty character or a whitespace character. While its meaning
may seem similar to NULL, it is stored and retrieved like any other character
in a text field. Empty strings are specific to string columns and cannot be
applied to different data types.
11
A NULL value represents the absence of a value or the unknown state of a field. It is
not the same as a blank or empty value. A column value becomes NULL when:
1. No Value is Provided:
When a row is inserted, and a value is not explicitly assigned to a column, it
defaults to NULL (if the column allows NULL values).
• If the Grade column is not specified, its value will be NULL (assuming no
default value is set).
12
An empty value (e.g., an empty string '' for text fields) indicates that a value is present,
but it contains no characters. A column value becomes empty when:
INSERT INTO Students (Student_ID, Name, Grade) VALUES (2, 'Bob', '');
14
• Example: A user leaves a text field blank, and the application saves it as ''
instead of NULL.
DATABASE CONCEPT
Structured Query Language (SQL)
• SQL, or Structured Query Language, is a standardized language used
to manage and manipulate data within relational databases.
• It allows users to perform various operations on data, including
retrieving, inserting, updating, and deleting information.
• SQL is essential for organizing, controlling, and accessing large sets
of structured data in applications ranging from small-scale business
software to large-scale enterprise systems.
16
DATABASE CONCEPT
Projection
• A certain column from a table is selected.
• Controls which columns to display, without filtering rows.
• SQL Keyword: SELECT column_name
Selection
• A subset of rows or records in a table is retrieved once it satisfies a selection
condition.
• Focus: Controls which rows to display, keeping all columns.
• SQL Keyword: WHERE
Joining
• In this operation, data are combined from two or more tables based on one or
more common column values.
• SQL Keyword: JOIN ON
17
CHAPTER 1
DATA QUERY LANGUAGE (DQL)
19
• Example:
SELECT name, description FROM department_list;
21
WHERE CLAUSE
• The WHERE clause is used to filter records. It is used to extract only those records that fulfill a
specified condition.
• Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
• Example:
SELECT *
FROM Employees
WHERE Position = ‘Manager’;
22
GROUP BY CLAUSE
• The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of
some functions. i.e. if a particular column has the same values in different rows then it will
arrange these rows in a group.
• Syntax:
SELECT column1, function_name(column2)
FROM table_name
GROUP BY column1, column2
• Example:
SELECT name, SUM(sal)
FROM emp
GROUP BY name;
23
GROUP BY CLAUSE
GROUP BY CLAUSE
The most commonly used SQL aggregate functions are:
HAVING BY CLAUSE
• Allows the filtering of query results based on aggregate functions and groupings. Unlike the
WHERE clause, which filters individual rows before any grouping occurs, the HAVING clause
works on groups of data that have been aggregated using functions like SUM(), AVG(), COUNT(),
and others.
• The HAVING clause is used to filter the result of GROUP BY based on the specified condition.
The conditions are Boolean type i.e. use of logical operators (AND, OR). This clause was included
in SQL as the WHERE keyword failed when we used it with aggregate expressions.
• Syntax:
SELECT column1, function_name(column2)
FROM table_name
GROUP BY column1, column2
HAVING condition
26
ORDER BY CLAUSE
• The ORDER BY statement in SQL is used to sort the fetched data in either ascending or
descending according to one or more columns.
• SQL ORDER BY default mode is sorting data into ascending order.
• Syntax:
SELECT * FROM table_name ORDER BY column_name ASC | DESC
• Syntax to use ORDER BY Clause with Column Number:
. . . ORDER BY Column_Number asc/desc
Example:
SELECT Roll_no, Name, Address FROM studentinfo ORDER BY 1
ORDER BY 1 means sorting values according to first column in the SELECT statement.
29
LIMIT CLAUSE
• The LIMIT clause allows you to specify the maximum number of records returned by a query. It is
commonly used for limiting query results when only a subset of the data is required, such as for
pagination, filtering top values, or analyzing a smaller portion of a large table.
• MySQL Syntax:
SELECT column_name(s) FROM table_name WHERE condition
LIMIT number;
• SQL/MS Access:
SELECT TOP number|percent column_name(s) FROM table_name
WHERE condition;
• Oracle 12:
SELECT column_name(s) FROM table_name ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY;
32
ALIASES
• Aliases are the temporary names given to tables or columns for the purpose of a particular SQL
query. It is used when the name of a column or table is used other than its original name, but the
modified name is only temporary.
o Aliases are created to make table or column names more readable.
o The renaming is just a temporary change and the table name does not change in the original
database.
o Aliases are useful when table or column names are big or not very readable.
o These are preferred when there is more than one table involved in a query.
• There are two types of aliases in SQL:
o Column Aliases: Temporary names for columns in the result set.
o Table Aliases: Temporary names for tables used within a query.
34
ALIASES
• A column alias is used to rename a column in the output of a query. Column aliases are useful for making the result
set more readable or when performing calculations or aggregations.
Syntax:
SELECT column_name AS alias_name FROM table_name;
Example:
SELECT EmployeeID AS ID FROM Employee;
or
SELECT EmployeeID AS “Employee ID” FROM Employee;
o column_name: The column name can be defined as the column on which we are going to create an alias name.
o alias_name: It can be defined as a temporary name that we are going to assign for the column or table.
o AS: It is optional. If you have not specified it, there is no effect on the query execution.
35
ALIASES
• A table alias is used when you want to give a table a temporary name for the duration of a query.
Table aliases are especially helpful in JOIN operations to simplify queries, particularly when the
same table is referenced multiple times (like in self-joins).
• Example: Table Alias for Joining Tables
We want to join the Customer table with itself to find customers who have the same
country and are aged 21. We’ll use table aliases for each instance of the Customer table
SQL OPERATOR
• SQL AND Operator. It allows you to filter data based on multiple conditions, all of which must
be true for the record to be included in the result set.
Syntax:
SELECT * FROM table_name
WHERE condition1 AND condition2 AND …conditionN;
• SQL OR Operator. It displays the records where any one condition is true, i.e. either condition1
or condition2 is True.
Syntax:
SELECT * FROM table_name
WHERE condition1 OR condition2 OR… conditionN;
37
Exercises:
1. Write a query to fetch all the records from the Student table where Age is 18 and ADDRESS is
Delhi.
2. Fetch all the records from the Student table where NAME is Ram or NAME is SUJIT.
38
SQL OPERATOR
• SQL LIKE Operator. It is used with the WHERE clause to search for a specified pattern in a
column. LIKE operator finds and returns the rows that fit in the given pattern. LIKE operator is
case-insensitive by default in most database systems. This means that if you search for “apple”
using the LIKE operator, it will return results that include “Apple”, “APPLE”, “aPpLe”, and so on.
Syntax:
SELECT column1, column2, … FROM table_name
WHERE column_name LIKE pattern;
SQL OPERATOR
Pattern Meaning
o Wildcards are used with the LIKE operator
‘a%’ Match strings that start with ‘a’
to search for specific patterns in strings.
Wildcard characters substitute one or more ‘%a’ Match strings with end with ‘a’
characters in the string. There are four
wildcard characters in SQL: Match strings that contain the start with ‘a’ and
‘a%t’
end with ‘t’.
▪ % (Percent): Represents zero or more
characters. Match strings that contain the substring ‘wow’ in
‘%wow%’
▪ _ (Underscore): Represents a single them at any position.
character. Match strings that contain the substring ‘wow’ in
‘_wow%’
▪ [] (Square Brackets): Represents any them at the second position.
single character within brackets.
Match strings that contain ‘a’ at the second
▪ – (Hyphen): Specify a range of ‘_a%’
position.
characters inside brackets.
Match strings that start with ‘a and contain at least
‘a_ _%’
2 more characters.
40
Exercises:
1. Retrieve SupplierID, Name, and Address from the suppliers table where the supplier name
starts with 'Ca’
2. Retrieve entire table, where address contains OKHLA.
41
SQL OPERATOR
• IN Operator. The IN Operator in SQL is used to specify multiple values/sub-queries in the WHERE
clause. It provides an easy way to handle multiple OR conditions.
o We only pass a single condition in the WHERE clause, however there might be situations where we
need to select data based on multiple conditions. For such cases, the IN operator is used.
o If any of the conditions are passed using the IN operator, they will be considered true.
o Syntax:
SELECT column_name FROM table_name
WHERE condition IN (condition_value1, condition_value2 …..);
o Example:
SELECT * FROM employee
WHERE Name IN (‘Ram’,’Suresh’);
Exercises:
1. Write a query to get Fname and Lname of personnel who have address in Almora and Ponta
2. Write a query to get Fname and Lname of personnel who do not have addresses in either Almora
or Ponta
43
SQL OPERATOR
• The SQL NOT operator is used to reverse the boolean result of a condition in SQL. It helps in
retrieving records that do not match a specific condition. It is mostly used to specify what should
not be included in the results table.
o Syntax:
SELECT column1, colomn2, …
FROM table_name WHERE NOT condition;
o Examples:
SELECT * FROM Customers WHERE NOT Country = 'UK’;
SELECT * FROM Customers WHERE NOT Country IN ('USA', 'UK’);
SELECT * FROM Customers WHERE NOT CustomerName LIKE 'R%’;
SELECT * FROM Customers WHERE NOT PostalCode IS NULL;
44
SQL OPERATOR
• NOT EQUAL Operator in SQL is used to compare two values and return if they are not equal.
This operator returns boolean values. If given expressions are equal, the operator returns false
otherwise true. If any one expression is NULL, it will return NULL.
o Note: <> and != perform the same operation i.e. check inequality. The only difference between
<> and != is that <> follows the ISO standard but != does not. So it is recommended to use <>
for NOT EQUAL Operator.
o Syntax:
SELECT * FROM table_name WHERE column_name != value;
SQL OPERATOR
• The IS NULL operator is used to check if a column contains a NULL value. If a column value is
NULL, the operator returns TRUE; otherwise, it returns FALSE. It's commonly used in WHERE
clauses to filter rows that contain NULL values in specific columns.
o A NULL value is different from a Zero Value and Blank Spaces. A field that has NULL value
means the field was left blank.
o Syntax:
SELECT * FROM table_name
WHERE column_name IS NULL;
o Example:
SELECT * FROM students
WHERE email IS NULL;
46
SQL OPERATOR
• The SQL BETWEEN operator is used to filter the result set within a specified range. It can be
applied to numeric, date, and text columns. The BETWEEN operator is inclusive, meaning it
includes the start and end values in the result set..
o Syntax:
SELECT column_name(s) FROM table_name
WHERE column_name BETWEEN value1 AND value2;
o Example:
SELECT * FROM products
WHERE price BETWEEN 10 AND 20;
47
SQL OPERATOR
• The SQL EXCEPT operator allows you to return the rows that exist in the first result set but not
in the second. It is useful for finding records in one table that do not have corresponding records
in another table.
o Note: The two SELECT queries must return the same number of columns and the data types
must be compatible.
o Syntax:
SELECT column_name(s) FROM table1 EXCEPT SELECT column_name(s) FROM
table2;
SQL OPERATOR
• The SQL UNION operator combines the results of two or more SELECT statements into one
result set. By default, UNION removes duplicate rows, ensuring that the result set contains only
distinct records.
o Rules for using the UNION OPERATOR:
▪ Each table used within UNION must have the same number of columns.
▪ The columns must have the same data types.
▪ The columns in each table must be in the same order.
• UNION ALL includes all rows, including duplicate rows.
o Syntax:
SELECT columnnames FROM table1 UNION SELECT columnnames FROM table2;;
50
SELECT Country
FROM Emp1
UNION
SELECT Country
FROM Emp2
ORDER BY Country;
51
CONCATENATION OPERATOR
• The concatenation operator in SQL is used to combine two or more strings or columns into a
single string. It is especially useful for creating meaningful outputs by joining text values from
different columns or adding literals (static text) to the output.
o Combines multiple columns or strings into one.
o Works with different data types (text, numbers, dates).
o Enhances query output by making it more readable.
o Syntax: SELECT concat(column1,column2, …) AS new_column FROM table_name;
o Example:
SELECT id, first_name, last_name, concat(first_name, ‘ ‘, last_name)
AS full_name, salary
FROM myTable;
52
FUNCTIONS IN SQL
• Categories:
o String functions. They are used to manipulate and transform the text string.
o Numeric functions. Accept numeric values, perform a mathematic operation on the values
and return resulting sheet
o Date and Time function. Used to manipulate and transform date and time data stored in
tables. Date functions operate on values of the DATE datatype.
• Some commonly used string functions: 53
o CONCAT(). Concatenates two or more strings together.
• Syntax: CONCAT(string1, string2, ...)
• Example: SELECT CONCAT('Hello', ' ', 'World!’); --Output: Hello World!
o HOUR(), MINUTE(), SECOND(). Extracts the hour, minute, or second from a time value.
• Syntax: HOUR(time_value) / MINUTE(time_value) / SECOND(time_value)
• Example: SELECT HOUR('[Link]'), MINUTE('[Link]'), SECOND('[Link]');
• Output: 14, 30, 25
SUBQUERIES
• A subquery is embedded inside another query and acts as input or output for that query.
Subqueries are also called inner queries and they can be used in various complex operations in
SQL.
• It can be used with an outer query which is used as input to the outer query. It can be used with
SELECT, FROM, and WHERE clauses.
• It is executed first before the execution of the outer query.
63
• Example: Let's select employees from sq_employee table with the department id as 1.
64
• Example: Let's select employees whose salary is less than average salary of all employees.
• Let’s select employees whose salary is greater than or equal to average salary of all employees.
65
• NOT IN operator: Let’s select all employees whose department is not in department table.
66
Correlated Subquery
• Correlated subquery is the one which uses columns from outer table for execution.
• Example: Select EmpId and Name of employee from sq_employee where salary is less than
average salary and department is same as outer table.
68
• EXISTS operator is used in MySQL to test for the existence of any record in a subquery. EXISTS
Operator works with a parent query and a child query. A parent query will execute if the child
query returns any value.
• Example: Select employees for which there exists at least 1 department where department of
employee is same as department in departments.
69
Step-by-Step Explanation:
1. The query looks at each customer in the customers table.
2. For each customer, it runs the subquery:
SELECT 1 FROM orders o WHERE o.customer_id = [Link];
• If the subquery finds at least one matching order, EXISTS returns TRUE, and that
customer is included.
• If no matching order is found, EXISTS returns FALSE, and that customer is excluded.
REFERENCES
• What is a relational database?: [Link]
• [Link]
• [Link]
• [Link]
THANK
YOU