0% found this document useful (0 votes)
18 views

DATABASE

...... computer science class 12

Uploaded by

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

DATABASE

...... computer science class 12

Uploaded by

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

DATABASE CONCEPT

 What is data?
Data is collection of characters, numbers and other symbols that represents values
of some situations or variables

Data Processing Information

Some examples of Data:

 Name, age, gender, contact, details etc of a person


 Images, graphics, animation, audio, video
 Online post, comment and messages
 111Signals generated by sensors
 Documents and web pages etc.

 WHAT IS META DATA?

Data about data is called Meta data.

 What is Database?
Collection of logically related data that has been recorded, organized and made
available for searching is called database.

Properties of database:
 It represents real world accepts.
 It is well structured and populated with specific data.
 It can be managed manually or automatically.
 It is secured and durable(data is never lost)

 Use of Database in real life applications

 Banking
 Online shopping
 Inventory management
 Loan and investment
 Reservation system and many more

 What are the needs for a database?


 To manage large amount of data without redundancy (repetition).

 To manage large amount of data without complexity.

 To maintain data uniformly in different places or file.

 Controlled data sharing at different user level.

DBMS

Database Management System is a software or technology used to manage data from a


database.

Some popular databases are My SQL, Oracle, Mongo DB, etc.

DBMS provides many operations e.g. creating a database, sorting in the database, updating an
existing database, delete from the database.

DBMS is a say system that enables you to store, modify and retrieve data in an organized way. It
also provides security to the database.

As the name suggests, the database management system consists of two parts.

They are:

1. Database

2. Management System

To find out what database is, we have to start from data, which is the basic building block of
any DBMS.

Data: Facts, figures, statistics etc. having no particular meaning (e.g. 1, SONIKA, 19 etc).

Record: Collection of related data items, e.g. in the above example the three data items had
no meaning. But if we organize them in the following way, then they collectively represent
meaningful information.

ROLL NAME AGE


1 SONIKA 19

Table or Relation: Collection of related records is called Table or Relation.

ROLL NAME AGE

1 SONIKA 19

2 SAKSHI 18

3 MANJU 21
Fields, Attributes or Domains

The columns of this relation are called Fields, Attributes or Domains.

Tuples or Records

The rows are called Tuples or Records.

1. Database:

Collection of related relations is called Datbase.

Table-1 Table-2

ROLL NAME AGE ROLL NO ADDRESS


1 SONIKA 19 1 BUHANA
2 SAKSHI 18
3 MANJU 21 2 SINGHANA

Table-3 Table-4
YEAR HOSTEL
ROLL NO YEAR
1 1997 2001 H1
2 2001
2006 H2
3 2006
We now have a collection of 4 tables. They can be called a “related collection” because we can
clearly find out that there are some common attributes existing in a selected pair of tables.
Because of these common attributes we may combine the data of two or more tables together
to find out the complete details of a student. Questions like “Which hostel does the youngest
student live in?” can be answered now,

Application Programs

DBMS Software
Stored
Database &
Its Definition

Fig. DBMS Environment

2. Management System:
A database-management system (DBMS) is a collection of interrelated data and a set of
programs to access those data. This is a collection of related data with an implicit meaning and
hence is a database.

The collection of data, usually referred to as the database, contains information relevant to an
enterprise. The primary goal of a DBMS is to provide a way to store and retrieve database
information that is both convenient and efficient. By data, we mean known facts that can be
recorded and that have implicit meaning.

The management system is important because without the existence of some kind of rules and
regulations it is not possible to maintain the database. We have to select the particular
attributes which should be included in a particular table; the common attributes to create
relationship between two tables; if a new record has to be inserted or deleted then which tables
should have to be handled etc. These issues must be resolved by having some kind of rules to
follow in order to maintain the integrity of the database.

Database systems are designed to manage large bodies of information. Management of data
involves both defining structures for storage of information and providing mechanisms for the
manipulation of information. In addition, the database system must ensure the safety of the
information stored, despite system crashes or attempts at unauthorized access.

Characteristics of Database Management Systems


The main characteristics of a DBMS are as follows:

1. Self-describing Nature of a Database System:


DBMS contains not only the database but also the description of the data that it stores.
This description of data is called metadata. Meta-data is stored in a database catalogue or
data dictionary. It contains the structure of the data and also the constraints that are
imposed on the data.
2. Insulation Between Programs and Data:
Since the definition of data is stored separately in a DBMS, any change in the structure of
data would be done in the catalogue and hence programs which access this data need not
be modified. This property is called Program-Data Independence.
3. Sharing of Data: A multiuser environment allows multiple users to access the database
simultaneously. Thus a DBMS must include concurrency control software to allow
simultaneous access of data in the database without any inconsistency problems.
NOTE: DBMS are work on client- server method.

View of Data
A major purpose of a database system is to provide users with an abstract view of the data.
That is, the system hides certain details of how the data are stored and maintained.

Data Abstraction

Many database-system users are not computer trained developers hide the complexity from
users through several levels of abstraction, to simplify users’ interactions with the system:

DBMS has three views (level):

1. User View /External View


2. Logical view / Conceptual View
3. Physical view / Internal View

User view User view / external view

MS(software) Conceptual view

DB [Table] Physical view

1. Physical view (or Internal View / Schema):


The lowest level of abstraction describes how the data are actually stored. The physical
view describes complex low-level data structures in detail.
2. Logical view (or Conceptual View / Schema):
The next-higher level of abstraction describes what data are stored in the database, and
what relationships exist among those data.
3. User view (or External View / Schema):
The highest level of abstraction describes only part of the entire database. Even though
the logical level uses simpler structures, complexity remains because of the variety of
information stored in a large database.

Advantages of using DBMS Approach:


1. Reduction in Redundancy:
There is no repetition of the same data. This also reduces the cost of storing data on
hard disks or other memory devices.
2. Improved Consistency:
Database are also reduced as there is a single copy of data that is accessed or updated
by all the users.
3. Improved Availability:
Same information is made available to different users. This helps sharing of information
by various users of the database.
4. Improved Security:
It is secured and durable (data is never lost).
5. User Friendly:
Usi
ng a DBMS, it becomes very easy to access, modify and delete data.

Limitations of using DBMS Approach

The two main disadvantages of using a DBMS:

1. High Cost:
The cost of implementing a DBMS system is very high. It is also a very time
consuming process which involves user requirements, designing the database
specifications, writing application programs and then also providing training.
2. Security and Recovery Overheads:
Unauthorized access to a database can lead to Database
Management Applications threat to the individual or organization depending on the
data stored
Also the data must be regularly backed up to prevent its loss due to fire,
earthquakes, etc.

Relational Database (RDBMS):----


Relational database developed by E.F Codd at IBM in 1970.

A Relational DBMS (RDBMS) is a DBMS which is based on the relational model.

It is used to organize collection of data as a collection of relations where each relation


corresponds to a table of values. Each row in the table corresponds to a unique
instance of data and each column name is used to interpret the meaning of that data in
each row.

Examples of RDBMS are Oracle, MySQL, IBM DB2.

For example, consider EMPLOYEE table in each row in this table represents facts about
a particular employee. The column (attributes) names – Name, Employee_ID, Gender,
Salary and Date_of_Birth specify how to interpret the data in each row.

Name Employee_ID Gender Salary Date_of_Birth


Radhika Mehta 1121 Female 20000 04-03-1990
Paras Bansal 2134 Male 25000 19-10-1993
Sarika yadav 3145 Female 20000 23-11-1992

Employee Table

In relational model:---

A row is called a Tuple.

A column is called an Attribute.

A table is called as a Relation.

The data type of values in each column is called the Domain.

The number of attributes in a relation is called the Degree of a relation.

The number of rows in a relation is called the Cardinality of a relation.

Relation Schema R is denoted by R (A , A , A …, A ) where R is the relation name 1 2 3,


n and A , A , A ,….A is the list of attributes. 1 2 3 n

Relation State is the set of tuples in the relation at a point in time.

Hence in above table:---

EMPLOYEE

table is a relation.

There are three tuples in EMPLOYEE relation.

Name, Employee_ID, Gender, Salary, Date_of_Birth are attributes.

The domain is a set of atomic (or indivisible) values.

The domain of a database attribute is the set of all the possible values that attribute
may contain. In order to specify a domain, we specify the data type of that attribute.
Following are the domain of attributes of the EMPLOYEE relation:

(a) Name – Set of character strings representing names of persons.

(b) Employee_ID–Set of 4-digit numbers

(c) Gender – male or female

(d) Salary – Number

(e) Date_of_Birth – Should have a valid date, month and year. The birth year of the
employee must be greater than 1985. Also the format should be dd-mm-yyyy.

The degree of the EMPLOYEE relation is 5 as there are five attributes in this relation.

The cardinality of the EMPLOYEE relation is 3 as there are three tuples in this relation.

Relation Schema – EMPLOYEE (Name, Employee_ID, Gender, Salary, Date_of_Birth)


Some More Characteristics of Relations:
Name Employee_ID Salary Date_of_Birth

Sarika yadav 3145 Female 20000 23-11-1992


Paras Bansal 2134 Male 25000 19-10-1993
Radhika Mehta 1121 Female 20000 04-03-1990

1. Ordering of tuples is not important in a Relation.


2. The ordering of attributes is also unimportant.
3. No two tuples of relation should be identical i.e. given any pair of two tuples, value
in at least one column must be different.
4. The value in each tuple is an atomic value (indivisible).
5. If the value of an attribute in a tuple is not known or not applicable or not available, a
special value called null is used to represent them.

RDBMS Constraints:---
NOTNULL: A column cannot have NULL values where NULL means missing / unknown
/ not applicable value.

UNIQUE: That all the values in a column are distinict / unique.

DEFAULT: A default value specified for the columns if no value is provided.

Key Constraint:

(i) Super key:-- Super key is a set of attributes in a relation, for which no two
tuples in a relation state have the same combination of values.
Every relation must have at least one super key which is the combination
of all attributes in a relation.

In EMPLOYEE relation, following are some of the super keys:


(a) {Name, Employee_ID, Gender, Salary, Date_of_birth}--default
superkey consisting of all attributes.
(b) {Name, Employee_ID, Date_of_Birth}
(c) {Employee_ID, Gender, Salary}
(d) {Name, Employee_ID, Gender}
(e) {Employee_ID}

NOTE- Gender and Salary attributes is not a super key because both these attributes have
identical value for employees Radhika Mehta and Sarika yadav

(ii) Candidate key:-- minimal super keys are called candidate keys.
Candidate key allow us to identify uniquely a tuple in the relation.
(iii) Primary Key:-- One of the candidate keys may be designated as Primary
key. Primary key is used to identify tuples in a relation. If a relation has
many candidate keys it is preferable to choose that one as primary key
which has least number of attributes.
Primary key allow us to identify uniquely a attributes in the relation. and
not null.
(iv) Foreign key:---The attributes which refers to value of an attribute as
primary key in another table is called Foreign key.

Super key Primary key


Candidate
key

Fig: Superkey, Key, Candidate key and Primary key

Structured Query Language (SQL):


SQL is a language that is used to manage data stored in a RDBMS. “The structured query
language SQL is the most popular query language used by major relational database
management system such as MY SQL, ORACLE etc.

We can create and interact with a database using SQL easily.

My SQL is an open source RDBMS software.

SQL is product of Micro-soft But My SQL is Product of ORACLE.

SQL language are 4th generation language.

SQL language are non-procedure language and Domain based language.

SQL commands
SQL commands used to define and modify a database.

Help of SQL commands we are create database, create relations and delete database,add
attributes , remove attributes and change schema

In others words we can say help of SQL commands modify database.

Command of SQL
Commands

DDL DML DCL TCL

Commonly used data types


Data type Meaning Example
CHAR (n) Fixed length character string. CHAR(5): “Vijay”
'n' is the number of characters
VARCHAR(n) Variable length character VARCHAR(15): “Ramu 123”
string. 'n' is the maximum
number of characters in the
string.
DATE Date in the form of YYYY- DATE: '2024-03-22’
MM-DD
INTEGER Integer number ‘23’ ‘125’
DECIMAL (m, d) Fixed point number m DECIMAL(5,2) : 999.99 -
represents the number of 567.78 DECIMAL (5) : 23456
significant digits that are 99999
stored for values and d
represents the number of
digits that can be stored
following the decimal point. If
d is zero or not specified then
the value does not contains
any decimal part

DATA DEFINATION LANGUAGE (DDL):


DDL are used to create the structured (schema) of dbms.

Help of DDL the made schema of DB (Table). In other words we can say the create the table
by DDL.

DDL are used to create the format of table.

In DDL mainly three commands are:

1. Create commands.
2. Alter commands.
3. Drop commands.
1. Create commands:

Creating Database:

To create a database we will give CREATE keyword Data base command.

Syntax

>> CREATE database <data base name>

Example

>> CREATE database< StudentAttendance >

Database is create this database name is StudentAttendance.


Creating Table:

After creating a database StudentAttendance we need to define relation in this database and
specify attributes for each relation along with data type and constraint.

Syntax

CREATE TABLE table name ( <attribute name> <attribute type> <constraint> );

Example

CREATE TABLE Students (Roll_no. int primary key );

Program:

CREATE TABLE Students(

Roll_no int primary key,

Name char(20) not null,

Address varchar(30) not null,

Mob_no varchar (13)

);

OUTPUT

Students

Roll_no Name Address Mob_no

2. Alter commands:
We need to add /remove an attribute or to modify the data type of an existing attribute or
to add constraint in attribute of the table by using the alter command.

“Alter TABLE statement is used to make changes in the structure of a table like
adding, removing or changing data type of columns.

Adding a column:
We may need to add an additional attribute in a table. it can be using the ADD
attribute statement.

Syntax:
ALTER TABLE Table name ADD <attribute name 1 , attribute name 2>
EXAMPLE:

Suppose we want to add a column Student_id , DOB in Students table. Following


command is used to add the column:

>> ALTER TABLE Students ADD Student_id;


Add primary key to a relation:

ALTER TABLE Table name Add primary key ( admission date, roll no );

Add foreign key to a relation:


When add foreign key to a relation some points need while adding foreign key.

The referenced relation must be already created.


 The referenced attribute must be part of a relation.

Syntax

ALTER TABLE Table name Add FOREIGN KEY (attribute name) REFERENCES
referenced Table name (attribute name);

Remove an attribute to a relation:

Using ALTER command we can remove attributes from a table.

Syntax:

ALTER TABLE table_name Drop attribute_name;

3. DROP commands:
We can use a DROP command to remove a database or a table permanently from the
system.
DROP command is used to delete tables.

Syntax to drop a table:


DROP TABLE Table_name;
Ex- DROP TABLE Students;

DATA MANIPULATION LANGUAGE (DML)


DML is used to insert, modify and delete data in a database.

Data manipulation using a database means either insert of new data removal of exiting
data or modification of existing data in the database.

In DML mainly four commands are:

(i) INSERTION COMMAND


(ii) SELECT COMMAND
(iii) UPDATE COMMAND
(iv) DELETE COMMAND
1. INSERTION COMMAND:

INSERT INTO Statement is used to insert new records in table.


Syntax:
INSERT INTO Table_name values (value1,value2….);
Value 1 corresponds to attribute 1 ,value 2 corresponds to attribute 2 and so on…..

For ex. The insert statement add the first record in the table.
INSERT INTO Students VALUES (101,"vikash yadav", "alwar", 9521081323 );

2. SELECT COMMAND:

The SQL command SELECT is used to retrieve data from the table in a database and
output is also displayed in tabular form.

There are various ways in which the SELECT command can be used.

Syntax of SELECT Command:

SELECT < attribute list>


FROM Table_name
WHERE Condition;
The FROM clause is always written with SELECT clause as specifies the name of the
table from which data is to be retrived.

To select all the data available in a table we use the following SELECT statement.
SELECT FROM Table_name;

SELECT command that identifies the tuples to be selected.


Bases on SELECT COMMAND we are create queries in database (relations).

Consider the following tables in the School Database for all the queries that follow:
PROGRAM
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,
Year_of_Birth DATE,
Dept_No INTEGER,
CONSTRAINT TEACHER_PK PRIMARY KEY (Teacher_ID),
CONSTRAINT TEACHER_FK FOREIGN KEY (Dept_No) REFERENCES
Department (Dept_ID)
);

The Teacher table refers Department table to keep track of the department to which a teacher
belongs.

Assume that the following data has been inserted in the Department tables using the Insert
commands:

INSERT INTO Department VALUES (1," Chemistry ");

INSERT INTO Department VALUES (2," Computer science ");

INSERT INTO Department VALUES (3," English");

INSERT INTO Department VALUES (4," Physics");

INSERT INTO Department VALUES (5," Biology");

INSERT INTO Department VALUES (6," Commerce");

INSERT INTO Department VALUES (7," Mathematics");

INSERT INTO Department VALUES (8," Economics ");

Department

Dept_ID Dept_Name
1 Chemistry
2 Computer
Science
3 English
4 Physics
5 Biology
6 Commerce
7 Mathematics
8 Economics

Assume that the following data has been inserted in the Teacher tables using the Insert
commands:

INSERT INTO Teacher VALUES (101,"subhash","chndr",'M',50000,2000,8);

INSERT INTO Teacher VALUES (102,"Ashok","sir",'M',45000,2001,5);

INSERT INTO Teacher VALUES (103,"Mahipal","ji",'M',60000,2002,7);

INSERT INTO Teacher VALUES (104,"Sunil"," Yaduvanshi ",'M',44000,2003,1);

INSERT INTO Teacher VALUES (105,"Imran","Khan",'M',46000,2004,4);


INSERT INTO Teacher VALUES (106,"Reena","Mam",'F',44000,2005,5);

INSERT INTO Teacher VALUES (107,"Vikash","Yadav",'M',46000,2006,2);

INSERT INTO Teacher VALUES (108,"Sandeep","Soni",'M',30000,2007,3);

INSERT INTO Teacher VALUES (109,"Manoj","Sharma",'M',32000,2008,6);

INSERT INTO Teacher VALUES (110,"Anand","Nehra",'M',28000,2009,2);

INSERT INTO Teacher VALUES (111,"Abbas","Alli",'M',45000,2010,7);

INSERT INTO Teacher VALUES (112,"Priya"," Rajput ",'F',41000,2011,3);

TEACHER

Teacher_ID First_Name Last_Name Gender Salary Year_of_Birth Dept_No


101 Subhash Chndr M 50000 2000 8

102 Ashok Sir M 45000 2001 5


103 Mahipal Ji M 60000 2002 7
104 Sunil Yaduvanshi M 44000 2003 1
105 Imran Khan M 46000 2004 4
106 Reena Mam F 44000 2005 5
107 Vikash Yadav M 42000 2006 2
108 Sandeep Soni M 30000 2007 3
109 Manoj Sharma M 32000 2008 6
110 Anand Nehra M 28000 2009 2
111 Abbas Alli 45000 2010 7
M
112 Priya Rajput F 41000 2011 3

Query: To retrieve all the information about Teacher with ID=101.

In this query we have to specify all the attributes in the SELECT clause.

NOTE -- use asterisk <Star> (*), which means select all the attributes in table.

Query (PROGRAM) 

SELECT *

FROM Teacher

WHERE Teacher_ID=101;

Result:

Teacher_ID First_Name Last_Name Gender Salary Year_of_Birth Dept_No


101 Subhash Chndr M 50000 2000 8

Query (PROGRAM) 
SELECT First_Name, Salary,Dept_No

FROM Teacher

WHERE Teacher_ID=105;

Result:

First_Name Salary Dept_No


Imran 46000 4

Query:

SELECT First_Name,Last_Name

FROM Teacher

WHERE salary > 45000;

Result:
First_Name Last_Name
Subhash Chndr
Mahipal Ji
Imran Khan
Vikash Yadav

Query: To display Teacher_ID,First_Name,Last_Name and Dept_No of teachers who


belongs to department number 4 or 7.

Program

SELECT Teacher_ID,First_Name,Last_Name, Dept_No

FROM Teacher

WHERE Dept_No = 3 OR Dept_No = 7;

Result:

Teacher_ID First_Name Last_Name Dept_No


103 Mahipal Ji 7
108 Sandeep Soni 3
111 Abbas Alli 7
112 Priya Rajput 3

NOTE : Thus Boolean operations AND,OR can also be used in the WHERE clause.

Query:

To retrieve names of all the teachers and the names and numbers of their respective
departments.
Note  That the above query requires two tables – Teacher and Department.

Query:

SELECT First_Name, Last_Name, Dept_ID, Dept_Name

FROM Teacher, Department;

This query we have not specified any WHERE clause, this query will combine each row in
Teacher table with each row of Department table resulting in product of two tables. This is
called as Cross Join.

Now if we have to combine each teacher with his/her respective department, there should be a
connecting column between the two tables which can be used to join them. Thus Dept_No
(Teacher table) and Dept_ID (Department table) can be used to join the two tables. This type
of join will result in joining rows from Teacher and Department table.

Based on the equality between Dept_No and Dept_ID columns.

Query:

SELECT First_Name, Last_Name, Dept_ID, Dept_Name

FROM Teacher, Department

WHERE Dept_ID=Dept_No;

Here Dept_ID=Dept_No is the join condition

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'.

For example, query will list all the Department numbers corresponding to departments having male
teachers.

SELECT Dept_No

FROM Teacher

WHERE GENDER ='M';

The result of the above query is:

Dept_No
8
5
7
1
4
2
3
6
2
7
In the above result, 2, 7 is appearing twice which is not required as the query is to find only the
department numbers.

Hence we can use DISTINCT keyword in the SELECT clause so that there is no repetition in
the result.

SELECT DISTINCT Dept_No

FROM Teacher

WHERE GENDER ='M';

The result of the above query is:

Dept_No
8
5
7
1
4
2
3
6

Renaming attributes(cloumn):

In case of we want to rename any column while display the output it can be done by using the
keyword ‘AS’.

Exampale:

SELECT Dept_ID, Dept_Name as SUBJECT_NAME

FROM Department;

OUTPUT:
Dept_ID SUBJECT_NAME
1 Chemistry
2 Computer
Science
3 English
4 Physics
5 Biology
6 Commerce
7 Mathematics
8 Economics

String pattern matching:


Sometimes it is required to match part of the string (Group of character).
This is called as string pattern matching. We can use 'LIKE' keyword along with two more
reserved characters - % (percent) and _ (underscore) for specifying single characters.

% replaces zero or more number of random characters and _ replaces a single character.

Query: To retrieve names of all the teachers starting from letter 'A'.

SELECT First_Name, Last_Name, Dept_No

FROM Teacher

WHERE First_Name LIKE "A%";

Output
First_Name Last_Name Dept_No
Ashok Sir 5
Anand Nehara 2
Abbas Alli 7

Query:

To retrieve names of all the teachers having 5 characters in the first name and starting with
'M'

Program:

SELECT First_Name

FROM Teacher

WHERE First_Name LIKE "M____";

Output:
First_Name
Manoj
Query: To retrieve names of all the teachers having at least 7 characters in the first name.

Program(syntax):

SELECT Last_Name

FROM Teacher

WHERE First_Name LIKE "_ _ _ _ _ _ _%";

Output:
Last_Name
Chndr
Ji
Soni
Sorting data in Dbms:
Sorting data can be done by using the clause – ORDER BY followed by the attributes which
needs to be sorted.

For ascending order the keyword ASC and for descending order the keyword DESC is used

Note: By default data (information) are sorted in ascending order.

Query:To list the names of teachers in alphabetical order.

Program:

SELECT First_Name

FROM Teacher

ORDER BY First_Name;

Output (Result):

----------------

Query: To list the names of all the Departments in the descending order of their names.

syntx

SELECT Dept_Name

FROM Department

ORDER BY Dept_Name DESC;

Result:

------------

Query:

To retrieve First name of all the teachers salary in descending order.

SELECT First_Name

FROM Teacher

ORDER BY salary DESC;

Output

-----------------

Query:

To retrieve the names of all the departments having Female teachers.

Program

SELECT DISTINCT Dept_Name

FROM Department
WHERE Dept_ID IN (Select Dept_No FROM Teacher WHERE Gender = 'F');

Output: -----------

This query is nested query.

Nested query: Query within another query is called Nested query. The outer one is called
outer query and the inner one is called as inner query.

Aggregate Functions:

When apply mathematical functions on group of values in a database. Such functions are
called Aggregate Functions.

Some commonly used 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.

Query: To find total salary of all the teachers .

Program-

SELECT SUM(Salary) AS Total_Salary

FROM Teacher;

Result: ________

Query: To find the maximum and minimum salary.

SELECT MAX(Salary) AS Max_Salary, MIN(Salary) AS Min_Salary

FROM Teacher;

Result:_________

Query: To count the number of teachers earning more than Rs 40000.

SELECT COUNT(Salary)

FROM Teacher

WHERE Salary > 40000;

Result: _________

Query: To retrieve the number of teachers in “Computer Science” Department.


SELECT COUNT(*) AS No_of_Computer_Science_Teachers

FROM Teacher

WHERE Dept_No = 3;

Result: __________

Arithmetic operators:-

We can also use arithmetic operators in the SELECT clause.

Arithmetic operator like as add, sub , divide, multiply, mode etc.

For example if we want to display Teacher name, current salary and a 10% increase in the
salary for those teachers who belongs to Department number 7, the SELECT statement can be
written:

SELECT First_Name, Last_Name, Salary, Salary*1.1 AS New_Salary

FROM Teacher

WHERE Dept_No = 7;

Result:_____

3. Update Command:
This command is used to update the attribute values of one or more tuples in a table.

Syntax :

UPDATE Table name

SET Attribute

WHERE Condition ;

Example : We want to update the Salary of teacher with Teacher_ID=105 to 60000.

Program-

UPDATE Teacher

SET Salary=60000

WHERE Teacher_ID=105;

OUTPUT:______________

Query : if we want to Update increase the salary of a Teacher Priya mam by 8000.

Program-

UPDATE Teacher

SET Salary=Salary+8000
WHERE First_Name="Priya";

4. DELETE COMMAND :
DELETE command is used to delete/ remove one or more records (Tuple) from a Table.

Syntax:

DELETE

FROM Table Name

WHERE Condition;

Query : If we want to delete the tuple for Teacher with ID=105

DELETE

FROM Teacher

WHERE Teacher_ID=105;

NOTE: If the WHERE clause is missing then it will delete all the tuples in a table :

DELETE FROM Teacher;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy