0% found this document useful (0 votes)
20 views56 pages

Dbms 1

The document provides an overview of database concepts, including definitions of data, information, and databases, as well as the need for database management systems (DBMS). It outlines the advantages and disadvantages of DBMS, various operations performed on databases, and the characteristics that ensure data integrity, security, and consistency. Additionally, it discusses the roles of different users in DBMS and contrasts traditional file management systems with relational databases.

Uploaded by

itzshrey10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views56 pages

Dbms 1

The document provides an overview of database concepts, including definitions of data, information, and databases, as well as the need for database management systems (DBMS). It outlines the advantages and disadvantages of DBMS, various operations performed on databases, and the characteristics that ensure data integrity, security, and consistency. Additionally, it discusses the roles of different users in DBMS and contrasts traditional file management systems with relational databases.

Uploaded by

itzshrey10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Class – XII

Subject: Information Technology(802)


Teacher’s name: Satinder Kaur

Vocational Skills

Unit – 1
Database Concepts
• DATA
Data is a collection of facts in a raw or
unorganized form such as numbers or
characters.
Example: Ankit, Delhi, 12, 80.
• INFORMATION
Information is data that has been
converted into a more useful,
meaningful form.
Example: Name – Ankit, City – Delhi,
Class – 12, Marks – 80.
• DATABASE
The related information when placed in an organized
form makes a database. We can organize related
information in the form of files or folders (File based
system) or in form of tables(Relational database).

• Properties of a Database
NEED FOR A DATABASE
1. Size of Data:
The small amount of data storing into spreadsheet is fine, however it might turns into a
large amount of data then Spreadsheet solution will not work. Even if the size of data
records goes into millions then storing data in multiple spreadsheet which will create a
problem of speed. It will take you long time to find a record from the multiple spreadsheet
files.

2. Ease of Updating Data:


Multiple peoples cannot edit the same file on same time. Other peoples must wait until
files are available to update which results into wastage of time. In a database, it is easy to
update data using various Data Manipulation languages (DML) available

3. Accuracy: When user doing data entry in files then it might be possible to enter incorrect
data due to no validation present like you can enter wrong spelling, wrong dates, and
wrong amount. So the Data accuracy is hard to maintain and accuracy is in question. A
database is pretty accurate as it has all sorts of build in constraints, checks etc. This means
that the information available in a database is guaranteed to be correct in most cases.
4. Security:
You cannot secure the data in the text files and spreadsheet. Anyone can access the file
and read any data present in the file. So storing data will not work with banking,
healthcare application, payroll department where privacy is difficult to maintain.
Databases have various methods to ensure security of data. There are user logins
required before accessing a database and various access specifiers. These allow only
authorised users to access the database.

5. Redundancy: The duplication of data can be possible using text files or spreadsheet.
Chances of adding multiple copies of data cannot be limited here. This will leads to
accuracy issues. Maintaining and updating multiple copies is not an easy task. This is
ensured in databases by using various constraints for data. Data integrity in databases
makes sure that the data is accurate and consistent in a database.

6. Incomplete Data: As data files are independent, accessing the information out of
multiple files becomes very difficult. To overcome above problem, associated with
storing data in the text file or spreadsheet the database is required.
A database management system or DBMS is a software used for creating and
managing the data in the database easily and effectively. It is basically a set of
programs that allow users to store, modify/update, and retrieve information from the
database as per the requirements.

• DBMS also provides security and protection to the database.


• DBMS acts as a middle layer between the database and the user.

Example: MySQL, MS SQL Server, Oracle, SQL, DB2, Microsoft Access, etc. are
different types of database management system.
Database Envoirnment

Empolyees are accessing Data through DBMS


VARIOUS OPERATIONS ON
DATABASE
There are multiple categories of operations that are performed on a
relational database file. The categories are
1. Defining the Database:- Data Definition operations - creating
database, files, file groups, tables, views etc
2. Manipulating the Database:- Data Manipulation operations - Insert,
Update, Delete data from objects
3. Sharing & Protecting the Database:- Data Control operations -
GRANT, REVOKE etc
4. Populating the Database:- Transaction Control Operations -
ROLLBACK,COMMIT
5. Maintaining the Database:- Database Maintenance Operations -
BACKUP, RESTORE,REBUILD etc
Characteristics of DBMS

There are various characteristics of a database management system, but following are some important characteristics:
1. Self-Describing Nature
Before DBMS, traditional file management system was used for storing information and data. There was no concept of
definition in traditional file management system like we have in DBMS. A DBMS should be of Self- Describing nature as it
not only contains the database itself but also the metadata. A metadata (data about data) defines and describes not
only the extent, type, structure and format of all data but also relationship between the data. This data represent itself
that what actions should be taken on it.
2. Support ACID Properties
Any DBMS is able to support ACID (Accuracy, Completeness, Isolation, and Durability) properties. It is made sure in
every DBMS that the real purpose of data should not be lost while performing transactions like delete, insert and
update. Let us take an example; if an employee name is updated then it should make sure that there is no duplicate data
and no mismatch of employee information.
• Atomicity − This property states that a transaction must be treated as an atomic unit, that is, either all of its
operations are executed or none. There must be no state in a database where a transaction is left partially completed.
• Consistency − The database must remain in a consistent state after any transaction. No transaction should have any
adverse effect on the data residing in the database
• Durability − The database should be durable enough to hold all its latest updates even if the system fails or restarts.
• Isolation − In a database system where more than one transaction are being executed simultaneously and in parallel,
the property of isolation states that all the transactions will be carried out and executed as if it is the only transaction in
the system. No transaction will affect the existence of any other transaction.
3. Concurrent Use of Database
There are many chances that many users will be accessing the data at the
same time. They may require altering the database system concurrently. At
that time, DBMS supports them to concurrently use the database without any
problem. For Example, the employees of railway reservation system can book
and access tickets for passengers concurrently. Every employee can see on his
own interface that how many seats are available or bogie is fully booked.
4. Transactions
Transactions are bunch of actions that are done to bring the database from
one consistent state to new consistent state. Traditional file-based system did
not have this feature. Transaction is always atomic that means it can never be
further divided. It can only be complete or incomplete.
For example, A person wants to credit money from his account to another
person’s account. Then transaction will be complete if he sends the money
and other guy receives the money. Anything other than this can lead to an
inconsistent transaction.
5. Data Persistence
Persistence means if the data is not removed explicitly then all the data will be
maintained in DBMS. If any system failure happens then life span of data
stored in the DBMS will be decided by the users directly or indirectly. Any data
stored in the DBMS can never be lost. If system failure happens in between
any transaction then it will be rolled back or fully completed, but the data will
never be at risk.
6. Backup and recovery
There are many chances of failure of whole database. At that time no one will
be able to get the database back and for sure company will be in a big loss.
The only solution is to take backup of database and whenever it is needed, it
can be stored back.
7. Data integrity
This is one of the most important characteristics of database management
system. Integrity ensures the quality and reliability of database system. It
protects the unauthorized access of database and makes it more secure. It
brings only the consistence and accurate data into the database.
8. Multiple Views
Users can have multiple views of database depending on their department and interest. DBMS
support multiple views of database to the users. For example, a user of teaching department will
have different view and user of hostel department will have different. This feature helps users to
have somewhat security because users of other department cannot access their files.
9. Stores any kind of data
A database management system is able to store any kind of data. It should not be restricted to
employee name, salary and address. Any kind of data that exists in the real world can be stored in
DBMS because we need to work with all kinds of data that is present around us.
10. Security
DBMS provides security to the data stored in it because all users have different rights to access the
database. Some of the user can access the whole database while other can access a small part of
database. For example, a computer teacher can only access files that are related to computer
subjects but HOD of the department can access files of all subject that are related to their
department.
11. Represents complex relationship between data
Data stored in a database is connected with each other and a relationship is made in between data.
DBMS is able to represent the complex relationship between data to make efficient and accurate
use of data.
Advantages of DBMS

1. Reducing Data Redundancy


The file based data management systems contained multiple files that were stored in
many different locations in a system or even across multiple systems. Because of this,
there were sometimes multiple copies of the same file which lead to data redundancy.
This is prevented in a database as there is a single database and any change in it is
reflected immediately. Because of this, there is no chance of encountering duplicate data.
2. Sharing of Data
In a database, the users of the database can share the data among themselves. There are
various levels of authorization to access the data, and consequently the data can only be
shared based on the correct authorization protocols being followed.
Many remote users can also access the database simultaneously and share the data
between themselves.
3. Data Integrity
Data integrity means that the data is accurate and consistent in the database. Data
Integrity is very important as there are multiple databases in a DBMS. All of these
databases contain data that is visible to multiple users. So it is necessary to ensure that
the data is correct and consistent in all the databases and for all the users.
4. Data Security
Data Security is vital concept in a database. Only authorised users should be allowed to
access the database and their identity should be authenticated using a username and
password. Unauthorised users should not be allowed to access the database under any
circumstances as it violates the integrity constraints.
5. Privacy
The privacy rule in a database means only the authorized users can access a database
according to its privacy constraints. There are levels of database access and a user can only
view the data he is allowed to. For example - In social networking sites, access constraints
are different for different accounts a user may want to access.
6. Backup and Recovery
Database Management System automatically takes care of backup and recovery. The users
don't need to backup data periodically because this is taken care of by the DBMS.
Moreover, it also restores the database after a crash or system failure to its previous
condition.
7. Data Consistency
Data consistency is ensured in a database because there is no data redundancy. All data
appears consistently across the database and the data is same for all the users viewing the
database. Moreover, any changes made to the database are immediately reflected to all
the users and there is no data inconsistency.
Disadvantages of DBMS

1. Cost
DBMS requires high initial investment for hardware, software and trained staff.
A significant investment based upon size and functionality of organization if
required. Also organization has to pay concurrent annual maintenance cost.
2. Complexity
A DBMS fulfill lots of requirement and it solves many problems related to
database. But all these functionality has made DBMS an extremely complex
software. Developer, designer, DBA and End user of database must have
complete skills if they want to user it properly. If they don’t understand this
complex system then it may cause loss of data or database failure.
3. Technical staff requirement
Any organization have many employees working for it and they can perform
many others tasks too that are not in their domain but it is not easy for them
to work on DBMS. A team of technical staff is required who understand DBMS
and company have to pay handsome salary to them too.
4. Database Failure
As we know that in DBMS, all the files are stored in single database so chances of database failure become more.
Any accidental failure of component may cause loss of valuable data. This is really a big question mark for big
firms.
5. Extra Cost of Hardware
A DBMS requires disk storage for the data and sometimes you need to purchase extra space to store your data.
Also sometimes you need to a dedicated machine for better performance of database. These machines and
storage space increase extra costs of hardware.
6. Size
As DBMS becomes big software due to its functionalities so it requires lots of space and memory to run its
application efficiently. It gains bigger size as data is fed in it.
7. Cost of Data Conversion
Data conversion may require at any time and organization has to take this step. It is unbelievable that data
conversion cost is more than the costs of DBMS hardware and machine combined. Trained staff is needed to
convert data to new system. It is a key reason that most of the organizations are still working on their old DBMS
due to high cost of data conversion.
8. Maintenance
As new threats comes daily, so DBMS requires to updates itself daily. DBMS should be updates according to the
current scenario.
9. Performance
Traditional files system was very good for small organizations as they give splendid performance. But DBMS gives
poor performance for small scale firms as its speed is slow
Types of user who play different
roles in DBMS
• Application Programmers
• Database Administrators
• End-Users
1. Application Programmers
The users who write the application programs in programming languages
(such as Java, C++, or Visual Basic) to interact with databases are called
Application Programmer.
2. Database Administrators (DBA)
A person who manages the overall DBMS is called a database administrator or
simply DBA.
3. End-Users
The end-users are those who interact with the database management system
to perform different operations by using the different database commands
such as insert, update, retrieve, and delete on the data, etc.
Applications of DBMS
What is File Management System?
• A file management system is a collection of programs that manage and store
data in files and folders in a computer hard disk.
• A file management system manages the way of reading and writing data to
the hard disk. It is also known as conventional file system.
• This system actually stores data in the isolated files which have their own
physical location on the drive, and users manually go to these locations to
access these files. It is the easiest way to store the data like text, videos,
images, audios, etc. in general files.
• Data redundancy is high in file management system, and it cannot be
controlled easily.
• Data consistency is not met, and the integration of data is hard to achieve.
• Operating System such as Linux and Windows has its own file system. For
example, NTFS is the Windows file system, and EXT is the Linux file system.
These operating systems provide less security to these files where they have
options such as hide files, locks, and sharing on files.
Relational Database
➢ Relational Model: The relational model
represents the database as a collection of
relations. A relation is nothing but a table of
values. Every row in the table represents a
collection of related data values. These rows in
the table denote a real-world entity or
relationship.
➢ Relational Database: A relational database
organizes data into tables which can be linked—
or related—based on data common to each. A
database in which the data is stored in the form
of relations (also called tables) is called a
Relational Database
Relational Database
Management System
• RDBMS: A Relational database management system (RDBMS) is a
database management system (DBMS) that is based on the relational
model as introduced by E. F. Codd. Some popular RDBMS software
available are: Oracle, MySQL, Sybase.

• Properties of RDBMS
 Values are atomic.
 All of the values in a column have the same data type.
 Each row is unique.
 The sequence of columns is insignificant.
 The sequence of rows is insignificant.
 Each column has a unique name.
 Integrity constraints maintain data consistency across multiple tables.
RDBMS TERMINOLOGY

➢ Relation/Table: A table refers to a two dimensional


representation of data arranged in columns (also called fields
or attributes) and rows (also called records or tuples).
➢ Column/Field/Attribute: Attributes are the properties which
define a relation. A column is a vertical entity in a table that
contains all information associated with a specific field in a
table.
For example, a column in the CUSTOMERS table is ADDRESS,
Address
which represents location description and would be as shown
below − Delhi

Chandigarh

Rajasthan

Ludhiana
• Row/Record/Tuple – A record is also called as a row of data is each
individual entry that exists in a table. For example, there are 7
records in the above CUSTOMERS table.
• Following is a single row of data or record in the CUSTOMERS table −
1 Ramesh 32 Delhi

• Degree: The total number of attributes in the relation is called the


degree of the relation.
• Cardinality: Total number of rows present in the Table.
• Entity : Something that exists apart from other things, having its
own independent existence
• Domain : An attribute is a name paired with a domain (nowadays
more commonly referred to as a type or data type)
• Relation schema: The overall structure of table is known as
Schema.
Database Languages

❖ Data Definition Language


The language is used to create database, tables, alter them, etc. With
this, you can also rename the database, or drop them. It specifies the
database schema.
The DDL statements include:
• CREATE: Create new database, table, etc.
• ALTER: Alter existing database, table, etc.(ADD, MODIFY,DROP)
• DROP: Drop the database
• RENAME: Set a new name for the table.
❖ Data Manipulation Language
The language used to manipulate the database like inserting data, updating table, retrieving
record from a table, etc. is known as Data Manipulation Language:
• SELECT: Retrieve data from the database
• INSERT: Insert data
• UPDATE: Update data
• DELETE: Delete all records
❖ Data Control Language
Grant privilege to a user using the GRANT statement. In the same way, revoke the privilege
using the REVOKE statement. Both of these statements come under the Data Control
Language (DCL).:
• GRANT: Give privilege to access the database.
• REVOKE: Take back the privilege to access the database.
❖ Transaction Control Language
Manage transactions in the Database using the Transaction Control Language:
• COMMIT: Save the work.
• SAVEPOINT: Set a point in transaction to rollback later
• ROLLBACK: Restores since last commit
SYNTAX of database commands

❖ DDL
1. CREATE DATABASE databasename;
2. CREATE TABLE table_name ( column1 datatype, column2 datatype,
column3 datatype, );
3. ALTER TABLE table_name ADD column_name datatype;
4. ALTER TABLE table_name DROP COLUMN column_name;
5. ALTER TABLE table_name ALTER COLUMN column_name datatype;
6. DROP TABLE table_name;
7. RENAME Old_table_name TO new_table_name;
❖ DML
1. SELECT column1, column2, ... FROM table_name;
2. SELECT * FROM table_name;
3. SELECT column1, column2, ... FROM table_name WHERE condition;
4. INSERT INTO table_name (column1, column2, column3, ...) VALUES
(value1, value2, value3, ...);
5. INSERT INTO table_name VALUES (value1, value2, value3, ...);
6. UPDATE table_name SET column1 = value1, column2 = value2, ...
WHERE condition;
7. DELETE FROM table_name WHERE condition;
❖ DCL
1. GRANT privileges_names ON object TO user;
2. REVOKE privileges ON object FROM user;

❖ TCL
1. COMMIT;
2. ROLLBACK;
3. SAVEPOINT SAVEPOINT_NAME;
4. ROLLBACK TO SAVEPOINT_NAME;
Constraints in RDBMS
Relational constraints are the restrictions imposed on the database
contents and operations. They ensure the correctness of data in the
database.

➢ Types of Constraints in RDBMS

1. Domain Constraint- Domain constraints are user defined data type.


Domain constraints specify that within each tuple, and the value of each
attribute must be unique. This is specified as data types which include
standard data types- integers, real numbers, characters, Booleans, variable
length strings, etc. we can define them like this:
Domain Constraint = data type + Constraints (NOT NULL / UNIQUE /
CHECK / DEFAULT)
❖ CHECK
Using the CHECK constraint we can specify a condition for a field, which should be satisfied at the time of entering
values for this field. For example, the below query creates a table Student and specifies the condition for the field AGE
as (AGE >= 18). That is, the user will not be allowed to enter any record in the table with AGE < 18.
CREATE TABLE Student
(
ID int (6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int NOT NULL CHECK (AGE >= 18)
);
❖ DEFAULT
This constraint is used to provide a default value for the fields. That is, if at the time of entering new records in the
table if the user does not specify any value for these fields then the default value will be assigned to them. For
example, the below query will create a table named Student and specify the default value for the field AGE as 18.
CREATE TABLE Student
(
ID int (6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int DEFAULT 18
);
❖ NOT NULL
If we specify a field in a table to be NOT NULL. Then the field will never accept null value. That is, you will be not
allowed to insert a new row in the table without specifying any value to this field. For example, the below query
creates a table Student with the fields ID and NAME as NOT NULL. That is, we are bound to specify values for these
two fields every time we wish to insert a new row.
CREATE TABLE Student (
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
ADDRESS varchar(20));
❖ UNIQUE
This constraint helps to uniquely identify each row in the table. i.e. for a particular column, all the rows should have
unique values. We can have more than one UNIQUE columns in a table. For example, the below query creates a tale
Student where the field ID is specified as UNIQUE. i.e, no two students can have the same ID.
CREATE TABLE Student
(
ID int(6) NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20)
);
2. Entity Integrity constraints

Entity integrity constraint ensures that the primary key attribute in a


relation, should not accept a null value.
❖ PRIMARY KEY Primary Key is a field which uniquely identifies each row
in the table. If a field in a table as primary key, then the field will not be
able to contain NULL values as well as all the rows should have unique
values for this field. So, in other words we can say that this is combination
of NOT NULL and UNIQUE constraints. A table can have only one field as
primary key .Below query will create a table named Student and specifies
the field ID as primary key.
CREATE TABLE Student
(
ID int(6) Primary key,
NAME varchar(10),
ADDRESS varchar(20)
);
3. Referential Integrity Constraints

Referential integrity ensures that the values for a set of attributes in


one relation must also appear the same for the particular set attributes
in another relation.
❖ FOREIGN KEY Foreign Key is a field in a table which uniquely
identifies each row of another table. That is, this field points to primary
key of another table. This usually creates a kind of link between the
tables. Consider the two tables as shown below:
 Foreign key
A foreign key is a key used to link two tables together. This is
sometimes also called as a referencing key. A Foreign Key is a column
or a combination of columns whose values match a Primary Key in a
different table.
Foreign Key Actions
When you set a foreign key constraint, you can control what happens
to the constrained column when the column it's referencing (the
foreign key) is deleted or updated.
The ON UPDATE and ON DELETE specify which action will execute
when a row in the parent table is updated and deleted. The following
are permitted actions: CASCADE, SET NULL, and RESTRICT
Delete actions of rows in the parent table
If you delete one or more rows in the parent table, you can set one of
the following actions:
• ON DELETE CASCADE: SQL Server deletes the rows in the child table
that is corresponding to the row deleted from the parent table.
• ON DELETE SET NULL: SQL Server sets the rows in the child table to
NULL if the corresponding rows in the parent table are deleted. To
execute this action, the foreign key columns must be nullable.
• ON DELETE RESTRICT: RESTRICT option will reject the delete
operation for the referenced table if there are one or more related
foreign key values in a referencing table.
Update action of rows in the parent table
If you update one or more rows in the parent table, you can set one of
the following actions:
• ON UPDATE CASCADE: SQL Server updates the corresponding rows in
the child table when the rows in the parent table are updated.
• ON UPDATE SET NULL: SQL Server sets the rows in the child table to
NULL when the corresponding row in the parent table is updated.
Note that the foreign key columns must be nullable for this action to
execute.
• ON UPDATE RESTRICT: RESTRICT option will reject the update
operation for the referenced table if there are one or more related
foreign key values in a referencing table.
Example:- CASCADE
ParentTable
Create table department
(
Deptid int(2) primary key,
Deptname varchar(20)not null
);
Child Table
Create table teacher
(
Teacherid integer primary key,
Name varchar(20) not null,
Salary int(5)default 40000,
Deptno integer,
Foreign key(deptno) references department(deptid)on delete cascade on update
cascade
);
SET NULL

alter table teacher add constraint fr foreign key(deptno) references department(deptid)on delete
set null on update set null;

Self-Referencing Tables:

A foreign key constraint can reference columns within the same table. These tables are called as
self-referencing tables. For example, consider a table Employee that contains five columns:
Employee_ID, Name, Age, Salary and Manager_ID. Because the manager is also an employee,
there is a foreign key relationship between the Manager_ID and Employee_ID as shown below:

CREATE TABLE Employee


(
Employee_ID INTEGER PRIMARY KEY,
Name VARCHAR(30),
Age INTEGER,
Salary DECIMAL(10,2),
Manager_ID INTEGER,
FOREIGN KEY (Manager_ID) REFERENCES Employee (Employee_ID)
);
 Adding a constraint with alter command

1. Alter table employee add constraint ntnull not null(Age);

2. Alter table teacher drop constraint teacher_fk;

 Functions

1. select teacherid, salary,deptno from teacher where deptno=4 or deptno=5;


2. select teacherid, salary,deptno from teacher where deptno=4 and deptno=5;
3. select sum(salary) from teacher;
4. select count(teacherid) from teacher;
Or
Select count(*) from teacher;
5. select max(salary) from teacher;
6. select min(salary) from teacher;
7. select avg(salary) from teacher;
8. select * from teacher where salary=30000;
 String pattern matching
• SQL pattern matching enables you to use _ to match any single character
and % to match an arbitrary number of characters (including zero
characters). In MySQL, SQL patterns are case-insensitive by default. Some
examples are shown here. Do not use = or <> when you use SQL patterns.

1. Select * from teacher where name like ”a%”;


2. Select * from teacher where name like ”%y”;
3. Select * from teacher where name like ”a_ _ _ _”;
To retrieve names of all the teachers having 6 characters in the first name
and starting with 'S'.
4. SELECT First_Name FROM Teacher WHERE First_Name LIKE "S_ _ _ _ _";
To retrieve names of all the teachers having at least 6 characters in the
first name.
5. SELECT First_Name FROM Teacher WHERE First_Name LIKE "_ _ _ _ _ _
%";
 Distinct

In SQL, duplicate tuples can appear more than once in a table and in the result of a query.
However if the requirement is to list distinct values of an attribute then this can be done
by using the keyword – 'DISTINCT'. The SQL DISTINCT keyword is used in conjunction with
the SELECT statement to eliminate all the duplicate records and fetching only unique
records. There may be a situation when you have multiple duplicate records in a table.
1. select distinct deptno from teacher;

 Sorting of data—order by clause


The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on
one or more columns. Some databases sort the query results in an ascending order by
default.
Desc=descending
1. select * from teacher order by name;
Or
Select * from teacher order by name asc;
2. select * from teacher order by name desc;
3. select * from teacher where name is null;
GROUP BY
The GROUP BY clause is a SQL command that is used to group rows that
have the same values. The GROUP BY clause is used in the SELECT
statement.
Query:-
1. Select d.deptid,t.teacherid,t.name t.salary,t.deptno from department
d,teacher t order by t.teacherid group by t.deptno;

HAVING
The HAVING Clause enables you to specify conditions that filter which
group results appear in the results. The HAVING clause was added to SQL
because the WHERE keyword could not be used with aggregate functions.
Query:-
1. Select deptno,deptname,teacherid, count(*) as No_of_teachers from
teacher,department where deptno=deptid group by deptno having
count(*)>1;
 Alias
SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to
make column/table names more readable. An alias only exists for the duration of the query.
 Suppose the teacher and department table both had same names for the department number, say
Dept_ID as shown below:
CREATE TABLE Department
( Dept_ID INTEGER PRIMARY KEY,
Dept_Name VARCHAR (30) NOT NULL
);
CREATE TABLE Teacher (
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_ID INTEGER,
CONSTRAINT TEACHER_PK PRIMARY KEY (Teacher_ID),
CONSTRAINT TEACHER_FK FOREIGN KEY (Dept_ID) REFERENCES Department (Dept_ID) );
In such case, when the join condition is specified, there will be an ambiguity about
which Dept_ID we are talking about. To resolve this problem, we have to prefix the
name of the attribute with the relation name followed by a period as shown in the
query below:
Query: To retrieve names of all the teachers who belong to Hindi department.
SELECT First_Name, Last_Name FROM Teacher, Department WHERE Department.
Dept_ID=Teacher. Dept_ID AND Dept_Name="Hindi";
Query:-
1. Select t.name,d.name from teacher t and department d where t.deptid=d.deptid;
Or
Select t.name,d.name from teacher t and department d;

We can also create alias to rename a column name as shown below:

2. Select max(salary) as maximum_salary from teacher;


Maximum_salary
45000
To display Teacher_ID,First_Name,Last_Name and Dept_No of teachers
who belongs to department number 4 or 7.

• SELECT Teacher_ID,First_Name,Last_Name, Dept_No FROM Teacher


WHERE Dept_No = 4 OR Dept_No = 7;
Result:
Teacher_ID First_Name Last_Name Dept_No

103 Surbhi Bansal 4

104 Megha Khanna 4

109 Neha Singh 7

• Thus Boolean operations AND,OR can also be used in the WHERE


clause.
To retrieve names of all the teachers who belong to Hindi department.

SELECT First_Name, Last_Name FROM Teacher, Department WHERE


Department. Dept_ID=Teacher. Dept_ID AND Dept_Name="Hindi";
First_Name Last_Name

Surbhi Bansal

Sometimes it is required to apply certain mathematical functions on group of values in a
database. Such functions are called Aggregate Functions. For example retrieving the
total number of teachers in all the Departments. Following are the commonly used built-
in aggregate functions:

• COUNT- It counts the numbers of tuples in the result of the query.

• SUM– It finds the sum of all the values for a selected attribute which has numeric data
type.

• MAX–It finds the maximum value out of all the values for a selected attribute which
has numeric data type.

• MIN-It finds the minimum value out of all the values for a selected attribute which has
numeric data type.

• AVG– It finds the average value of all the values for a selected attribute which has
numeric data type.
• SELECT SUM(Salary) AS Total_Salary , AVG(Salary) AS Average_Salary
FROM Teacher;
• SELECT MAX(Salary) AS Max_Salary, MIN(Salary) AS Min_Salary
FROM Teacher;
• SELECT COUNT(Salary) FROM Teacher WHERE Salary > 40000;
• SELECT COUNT(*)FROM Teacher WHERE Salary >40000;
• SELECT First_Name, Last_Name, Salary, Salary*1.1 AS New_Salary
FROM Teacher WHERE Dept_No = 4;
GROUP BY
The GROUP BY clause is a SQL command that is used to group rows that
have the same values. The GROUP BY clause is used in the SELECT
statement.
• Query:-
1.Select d.deptid,t.teacherid,t.name t.salary,t.deptno from department
d,teacher t order by t.teacherid group by t.deptno;

HAVING
The HAVING Clause enables you to specify conditions that filter which
group results appear in the results. The HAVING clause was added to SQL
because the WHERE keyword could not be used with aggregate functions.
• Query:-
1.Select deptno,deptname,teacherid, count(*) as No_of_teachers from
teacher,department where deptno=deptid group by deptno having
count(*)>1;
Question for Practice
Que 1: Consider the following Employee table:
• Table Name: Employee
Emploee_ Employee Job_title Salary Bonus Age Manager_
id _Name Id
1201 Diya President 50000 Null 29 Null
1205 Amtra Manager 30000 2500 26 1201
1211 Rahul Analyst 20000 1500 23 1205
1213 Manish Salesman 15000 Null 22 1205
1216 Megha Analyst 22000 1300 25 1201
1217 Mohit Salesman 16000 Null 22 1205

• The primary key of this table is Employee_ID and Manager_ID is


a foreign key that references Employee_ID.
Write SQL commands for the following:
1. Create the above table.
2. Insert values as shown above.
3. Delete the Employee having Employee_ID 1217.
4. Update the salary of “Amyra” to 40000.
5. Alter the table Employee so that NULL values are not allowed for Age column.
6. Write a query to display names and salaries of those employees whose salary are greater
than 20000.
7. Write a query to display details of employees who are not getting any bonus.
8. Write a query to display the names of employees whose name contains “a” as the last
alphabet.
9. Write a query to display the name and Job title of those employees whose Manager_ID is
1201.
10.Write a query to display the name and Job title of those employees whose Manager is
“Amyra”.
11.Write a query to display the name and Job title of those employees aged between 26
years and 30 years (both inclusive)
Q2. A Railway company uses machines to sell tickets. The machine
details and daily sales information are recorded in two tables:
Field DataType

Machine_Id CHAR(3) Table Name:Machine


Station CHAR(30)

Field DataType

Machine_Id CHAR(3)

Date DATE Table Name: Sales


Ticket_Sold INTEGER

Income DECIMAL(8,2)
• The primary key of the table Machine is Machine_ID. Records in the
table Sales are uniquely identified by the fields Machine_ID and
Date.

(a) Create the tables Machine and Sales.

(b) Write a query to find the number of ticket machines in each


station.

(c) Write a query to find the total ticket income of the station “New
Delhi” of each day.

(d) Write a query to find the total number of tickets sold by the
machine (Machine_ID = 122) till date.

You might also like