Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Database & SQL Class XII

Database
Database is required for keeping the information of an organization at one central place (not geographical
- consider distributed database on the NET) so that it can meet the requirements of different applications.
Database should be made in such a way that:
1. It can accept the required changes in future
2. It can be used by more than one independent applications
3. Avoid redundancy
4. Ensure data independence

Database is collection of interrelated files containing collection of records, where each record of file is
collection of logically related data items. Database overcomes the following problems faced in a
traditional file processing systems:
1. Data redundancy
2. Data inconsistency
3. Data not sharable
4. Non-standard data
5. Insecure data

There are four types of database models:


1. Hierarchical database
2. Network database
3. Relational database
4. Object Oriented database

Relation / Table / Database file


ENO NAME DESIG DOJ SAL MOB
1045 ADITYA JAIN MANAGER 2010-07-02 80000 99456972
1072 ALEX JOHN MANAGER 2007-04-09 75000 55012016
1032 RITA GUPTA MANAGER 2010-07-02 80000 66442288
1012 NITU BHAT DIRECTOR 2008-10-15 95000 55123765
1049 ALOKA SINGH DIRECTOR 2007-04-09 90000 64192837
1025 RITA GUPTA DIRECTOR 2008-10-15 95000 55876534
1017 ADITYA JAIN MANAGER 2008-09-03 75000 98283761

Relational Database: It is a collection of related tables / relations. The software required to handle or
manipulate these tables or relations is known as Relational Database Management System (RDBMS).
Oracle, DB2, Access, MySQL and Visual FoxPro are some commonly used RDBM.

Table / Relation: A collection of rows and columns form a table. A relation among set of values (in each
column / attribute) is represented by a row of a table, hence the term relational database. The horizontal
subset of a Table (Relation) is known as a Row (Tuple). The vertical subset of a Table (Relation) is
known as a Column (Attribute).
A relation in a database has the following characteristics:
1. Every value in a relation is atomic, that is, it cannot be further divided
2. Names of columns are distinct and order of columns is immaterial
3. The rows in the relation are not ordered
A relation is a set, elements in set are not ordered, no ordering defined on rows of relation.
Domain is a set of values from which actual values of an attribute of a Table (Relation) (i.e. the column
of relation obtains values from domain) are obtained.

FAIPS, DPS Kuwait Page 1 of 18 © Bikram Ally


Database & SQL Class XII
Relation (Table): Employee
ENO NAME DESIG DOJ SAL MOB
1045 ADITYA JAIN MANAGER 2010-07-02 80000 99456972
1072 ALEX JOHN MANAGER 2007-04-09 75000 55012016
1032 RITA GUPTA MANAGER 2010-07-02 80000 66442288 Record /
1012 NITU BHAT DIRECTOR 2008-10-15 95000 55123765 Row /
1049 ALOKA SINGH DIRECTOR 2007-04-09 90000 64192837 Tuple
1025 RITA GUPTA DIRECTOR 2008-10-15 95000 55876534
1017 ADITYA JAIN MANAGER 2008-09-03 75000 98283761

Field / Column / Attribute


Degree Cardinality
Number of Columns in a Table (Relation) Number of Rows in a Table (Relation)

An attribute having distinct values that can be used to identify a tuple (row) uniquely is known as Key.
A relation may have more than one such key. All the keys are known as Candidate Keys. Out of all the
possible Candidate keys, one is selected as Primary Key, and rest are called Alternate Keys. Attributes
or columns which are not Candidate Keys are known as Non-Key attributes.

Primary Key is an attribute (column) that contains distinct values which can be used to identify a tuple
(row) uniquely in a relation (table).

Non Key Attribute is an attribute which not a Candidate Key (attribute not having distinct values).

Relation: Employee
ENO NAME DESIG DOJ SAL MOB
1045 ADITYA JAIN MANAGER 2010-07-02 80000 99456972
1072 ALEX JOHN MANAGER 2007-04-09 75000 55012016
1032 RITA GUPTA MANAGER 2010-07-02 80000 66442288
1012 NITU BHAT DIRECTOR 2008-10-15 95000 55123765
1049 ALOKA SINGH DIRECTOR 2007-04-09 90000 64192837
1025 RITA GUPTA DIRECTOR 2008-10-15 95000 55876534
1017 ADITYA JAIN MANAGER 2008-09-03 75000 98283761

Non-key Attributes

Primary Key Candidate Keys Alternate Key

In the Employee relation, ENO and MOB are Key attributes. ENO is selected as Primary key, then
MOB is the Alternate key. Attributes NAME, DESIG, DOJ and SAL are non-key attributes.
FAIPS, DPS Kuwait Page 2 of 18 © Bikram Ally
Database & SQL Class XII
Operators of Relational Algebra (Operations that can be carried out on a relation):
1. Selection (Unary Operator): Selecting horizontal subset of a relation or selecting rows from a relation.

2. Projection (Unary Operator): Selecting vertical subset of a relation or selecting columns from a
relation.

3. Cartesian Product (Binary Operator): obtaining a new relation by pairing every rows of one relation
with every rows of the other relation. It operates on two or more relations and is denoted by X. For
example Cartesian Product of two relations R1 and R2 is represented by R1 X R2. The degree of the
new relation (R1 X R2) is equal to sum of degrees of relation R1 and relation R2. The cardinality of
the new relation (R1 X R2) is product of cardinality of relation R1 and cardinality of relation R2.
Example:

Relation: Student (R1) Relation: Teacher (R2)


ROLL SNAME CLASS TNAME SUBJECT
1 AKASH 12 MS. RITA GEOGRAPHY
5 DIPAK 11 MR. ANUP POLITICAL SC
24 RUPA 12
31 STUTI 11

Cartesian product of Student and Teacher: Student (R1) X Teacher (R2)


ROLL SNAME CLASS TNAME SUBJECT
1 AKASH 12 MS. RITA GEOGRAPHY
1 AKASH 12 MR. ANUP POLITICAL SC
5 DIPAK 11 MS. RITA GEOGRAPHY
5 DIPAK 11 MR. ANUP POLITICAL SC
24 RUPA 12 MS. RITA GEOGRAPHY
24 RUPA 12 MR. ANUP POLITICAL SC
31 STUTI 11 MS. RITA GEOGRAPHY
31 STUTI 11 MR. ANUP POLITICAL SC

4. Union (Binary Operator): Obtain a new relation by getting rows from both the relations. It operates
on two or more relations and is indicated by U. For example, R1 U R2 represents union operation
between two relations R1 and R2. The degree of the relation new relation (R1 U R2) is equal degree
of relation R1 (or degree of relation R2). The cardinality of the new relation (R1 U R2) is sum of
cardinality of relation R1 and cardinality of relation R2. Two relations are compatible for union
operation when:
a) Degree of relation R1 = Degree of relation R2
b) Corresponding attributes of the two relations (R1 and R2) must have a common domain

Example:
Relation: CS12A (R1) Relation: CS12B (R2) CS12A (R1) U CS12B (R2)
NAME MARKS GRADE NAME MARKS GRADE NAME MARKS GRADE
FATIMA 69.5 C1 ATUL 89.0 A2 FATIMA 69.5 C1
FARUQ 78.0 B2 EKTA 67.5 C2 FARUQ 78.0 B2
GAURI 95.5 A1 REKHA 88.5 B1 GAURI 95.5 A1
REKHA 89.0 A2
EKTA 67.5 C2
ATUL 88.5 B1
FAIPS, DPS Kuwait Page 3 of 18 © Bikram Ally
Database & SQL Class XII
SQL - Structured Query Language
DDL – Data Definition Language
DDL deals with structure of a table, which sets up, changes or removes data structures from the database.
A DDL command includes the word ‘table’ after the SQL command name. DDL commands include:
• Commands to create database or table
• Command to delete database or table
• Command to change structure of a table by adding column(s) or deleting column(s)

DML – Data Manipulation Language


DML includes those commands, which deals data stored in a table. DML is a set of commands that enables
users to access or manipulate data. By data manipulation, we mean:
• The retrieval of information stored in a table
• The insertion of new row in a table
• The deletion of row or rows from a table
• The editing of values stored in a row or rows

Commonly used Data Types of SQL


1. CHAR : Used to store textual data (characters). Text (either single character or string of characters)
are to be enclosed within a pair of single quote (') or pair of double quote (").
sec char – column name sec that will store single character.
area char (20) – column name area whose length is exactly 20 character. The concept of nul
terminating string is not valid for SQL.
Examples of char type data: 'Anil', "Priya", '371', "28.5", '***','A',"g"

2. NUMERIC: Used to store numeric data. Any number, either integer or floating point value (real
number) can be a numeric type.
empno numeric(4,0) / empno numeric(4) – column name empno that will store 4 digits integer
value (excluding sign)
marks numeric(5,1) – column name marks that will store a floating value, 5 significant digits, 4
p1aces before decimal point and 1 place after the decimal and significant digits does not include
decimal point).
Examples of numeric type data: 12, -30, 15.5, -9.5

3. DATE: Used to store date type data. A date type value is represented as YYYY-MM-DD. Date type
constant is enclosed with either pair single quote (') or pair of double quote ("). Delimiter for char
type and date are same but char type and date are treated differently.
Date can be entered as YYYY/MM/DD format but it will be stored as YYYY-MM-DD format.
dob date – column name dob is a date type where a date will be stored in YYYY-MM-DD format.
Examples of date type: "1995-09-11", '1947-08-15'

Some useful SQL Commands of MySQL


1. Before creating table or tables, a database has to be created. Without creating a database, one cannot
create any table. Creating a database will create a folder in the backing storage. The database (or the
folder) will act as container for the tables. Database is created only once, for the first time.
Note: Every SQL command is terminated by a semi-colon. SQL commands can be typed in any
case (UPPERCASE or lowercase or MIXEDcase)
Syntax for creating a database is CREATE DATABASE DATABASENAME;
Example: CREATE DATABASE CLASS12A;
CREATE DATABASE CLASS12B;
CREATE DATABASE EMPFILES;
FAIPS, DPS Kuwait Page 4 of 18 © Bikram Ally
Database & SQL Class XII
2. After creating the database, we need to use the database. For the first time, one need to create the
database and use the database. For the next time onwards, one need to use the database only.
Syntax for using a database is USE DATABASENAME;
Example: USE CLASS12A;
USE CLASS12B;
USE EMPFILES;

3. In case one has forgotten the name of the database, one can find the names all the databases stored in
the backing storage.
Syntax for listing all the database names is SHOW DATABASES;
Example: mysql>SHOW DATABASES;
Database
information_schema
class12
mysql
performance_schema
test

4. In case one has forgotten the names of the tables, one can find the names in the data base by using the
SQL command show tables. Show tables command works only after use command.
Syntax for listing all the table names is SHOW TABLES;
Example: mysql>USE CLASS12;
mysql>SHOW TABLES;
Tables_in_CLASS12
student
employee
classteacher
CS12A
CS12B

5. Sometimes we need to remove the database from the backing storage. Removing a database will delete
all the tables and other files which are stored under that database will also be removed.
Syntax for removing a database is DROP DATABASE DATABASENAME;
Example: mysql>DROP DATABASE CLASS12A;
mysql>DROP DATABASE CLASS12B;

Creating a new table in a database (DDL)

Syntax:
Create Table TableName (Col1 DataType1, Col2 DataType2, …);
Create Table command creates a new table without any rows (tuples).

Example:
create table student(roll numeric(2), name char(25), fees numeric(8,2),
dob date);
Name of the table is student. The table student will have 4 columns (attributes). Column roll will
be integer type (maximum 2 digit), column name will be character type (maximum 25 characters), column
fees will be floating point type (8 significant digits, 6 places before decimal point and 2 places after the
decimal point), column dob will be date type.

FAIPS, DPS Kuwait Page 5 of 18 © Bikram Ally


Database & SQL Class XII
Inserting a new row in a table (DML)

Syntax:
i) Insert Into TableName Values (Value1, Value2, Value3, …);
ii) Insert Into TableName (Col1, Col2,…) Values (Value1, Value2,…);
Insert Into command adds (inserts) a new row (tuple) in a table (relation).
Syntax i) is used when values are available for all the columns of a row.
Syntax ii) is used when values are available for selected columns of a row and remaining columns in the
will have value NULL.
Note: Values inside the parenthesis should match data type of the columns and number of values
should be equal to number of columns. If a value exceed range or date is invalid MySQL will
flag syntax error and the row will not be inserted in the table.

Examples:
i) Insert Into Student Values(21,'MANAS VERMA',7800,'1989-09-07');
Insert Into Student Values(7,'DHRUTI GUPTA',8800,'1988-02-28');
ii) Insert Into Student(Roll,Name,Fees) Values(16,'PRATAP RAO',7500);
Insert Into Student(Roll,Name,Fees) Values(2,'ANIL JAIN',8300);
Note: The column dob will have value NULL.

Adding new column(s) in the table (DDL)

Syntax:
Alter Table TableName ADD Col DataType;
Alter Table command adds a new column to an existing table.

Examples:
Alter Table Student ADD Class Numeric(2);
Alter Table Student ADD Sec Char;
Note: The columns Class and Sec will have value NULL.

Insert Into Student Values(18,'MOHD ALI',6800,'1989-01-31',12,'C');


Insert Into Student Values(4,'ASIT GARG',6900,'1988-07-10',12,'A');

Modifying the existing content of the table (DML)

Syntax:
Update TableName Set Col1=Value1, Col2=Value2, … Where Condition;
Note: Update command is used to makes changes in row (rows) of an existing table. With Where
Condition changes will made (values will be updated) in row(s) where Condition is true.
Without Where Condition changes will made (values will be updated) in all the row(s).
• Operator =, when used in a condition, compares two values.
• Operator =, when used with set will assign value to a column.

Example:
Update Student Set Class=12, Sec='A' Where Roll=16;
Update Student Set Fees=7800 Where Roll=16;
Update Student Set Dob='1989-04-18' Where Roll=31;
Update Student Set Sec='D' Where Sec='A';
Update Student Set Fees=1.1*Fees;
FAIPS, DPS Kuwait Page 6 of 18 © Bikram Ally
Database & SQL Class XII
Deleting a row/rows from a table (DML)

Syntax:
Delete From TableName Where Condition;
Note: Delete From removes row(s) from an existing table. With Where Condition, row(s)
where Condition is true will be deleted. Without Where Condition, all the row(s) will
be deleted.

Examples:
Delete From Student Where Roll=13;
Delete From Student Where Name='SUSHIL JAIN';
Delete From Student Where Roll=12 Or Roll=17;
Delete From Student Where Class=12;
Delete From Student;

Deleting a table (DDL)

Syntax:
Drop Table TableName;
Drop Table command deletes entire the contents of the table along with the structure of the table.
Examples:
Drop Table Student;
Drop Table Employee;

Table Student used for Select commands given below.


Roll Name Fees Dob Class Sec
21 MANISH SINGH 7800 1989-09-07 12 B
17 DHRUTI GUPTA 8800 1988-02-28 12 C
24 SUNIT KUMAR 7500 1989-08-24 12 A
16 PUNEET ARORA 7500 1990-05-12 12 A
33 SUBHOJIT SAHA 6800 1989-01-31 12 C
13 ASIT BHARGAV 8500 1989-04-29 12 B
10 SUSHIL JAIN 6900 1988-03-13 12 A
14 ARPITA SHETTY 7200 1988-06-15 12 C
27 SUNILA DESAI 7000 1990-11-29 12 B

Displaying the content from a table – SELECT (DML)

Syntax:
i) Select * From TableName Where Condition;
ii) Select Col1, Col2, Col3, … From TableName Where Condition;
Select commands retrieves data stored in a table. Columns could be an expression also. If Where
Condition is omitted then all the rows of a table are selected.

Examples:
Select * From Student;
Roll Name Fees Dob Class Sec
21 MANISH SINGH 7800 1989-09-07 12 B
17 DHRUTI GUPTA 8800 1988-02-28 12 C

FAIPS, DPS Kuwait Page 7 of 18 © Bikram Ally


Database & SQL Class XII
24 SUNIT KUMAR 7500 1989-08-24 12 A
16 PUNEET ARORA 7500 1990-05-12 12 A
33 SUBHOJIT SAHA 6800 1989-01-31 12 C
13 ASIT BHARGAV 8500 1989-04-29 12 B
10 SUSHIL JAIN 6900 1988-03-13 12 A
14 ARPITA SHETTY 7200 1988-06-15 12 C
27 SUNILA DESAI 7000 1990-11-29 12 B

Select Name From Student;


Name
MANISH SINGH
DHRUTI GUPTA
SUNIT KUMAR
PUNEET ARORA
SUBHOJIT SAHA
ASIT BHARGAV
SUSHIL JAIN
ARPITA SHETTY
SUNILA DESAI

Select Roll, Name, Class, Sec From Student;


Roll Name Class Sec
21 MANISH SINGH 12 B
17 DHRUTI GUPTA 12 C
24 SUNIT KUMAR 12 A
16 PUNEET ARORA 12 A
33 SUBHOJIT SAHA 12 C
13 ASIT BHARGAV 12 B
10 SUSHIL JAIN 12 A
14 ARPITA SHETTY 12 C
27 SUNILA DESAI 12 B

SELECT with WHERE clause (DML)

Select * From Student Where Roll>20;


Roll Name Fees Dob Class Sec
21 MANISH SINGH 7800 1989-09-07 12 B
24 SUNIT KUMAR 7500 1989-08-24 12 A
33 SUBHOJIT SAHA 6800 1989-01-31 12 C
27 SUNILA DESAI 7000 1990-11-29 12 B

Select * From Student Where Roll>=10 And Roll<=20;


Roll Name Fees Dob Class Sec
16 PUNEET ARORA 7500 1990-05-12 12 A
10 SUSHIL JAIN 6900 1988-03-13 12 A
17 DHRUTI GUPTA 8800 1988-02-28 12 C
13 ASIT BHARGAV 8500 1989-04-29 12 B
14 ARPITA SHETTY 7200 1988-06-15 12 C

FAIPS, DPS Kuwait Page 8 of 18 © Bikram Ally


Database & SQL Class XII
Select Roll, Name, Sec From Student Where Sec='A' Or Sec='B';
Roll Name Sec
21 MANISH SINGH B
24 SUNIT KUMAR A
16 PUNEET ARORA A
13 ASIT BHARGAV B
10 SUSHIL JAIN A
27 SUNILA DESAI B

Select Roll, Name, Sec From Student Where Class!=12;


Empty Set.
No output on the screen since no row could be selected which satisfied the condition Class!=12

Select * From Student Where Sec In ('A', 'C');


Roll Name Fees Dob Class Sec
17 DHRUTI GUPTA 8800 1988-02-28 12 C
24 SUNIT KUMAR 7500 1989-08-24 12 A
16 PUNEET ARORA 7500 1990-05-12 12 A
33 SUBHOJIT SAHA 6800 1989-01-31 12 C
10 SUSHIL JAIN 6900 1988-03-13 12 A
14 ARPITA SHETTY 7200 1988-06-15 12 C

Select * From Student Where Dob Between '1988-1-1' And '1988-12-31';


Roll Name Fees Dob Class Sec
17 DHRUTI GUPTA 8800 1988-02-28 12 C
10 SUSHIL JAIN 6900 1988-03-13 12 A
14 ARPITA SHETTY 7200 1988-06-15 12 C

Select * From Student Where Fees Between 7000 And 9000;


Roll Name Fees Dob Class Sec
21 MANISH SINGH 7800 1989-09-07 12 B
17 DHRUTI GUPTA 8800 1988-02-28 12 C
24 SUNIT KUMAR 7500 1989-08-24 12 A
16 PUNEET ARORA 7500 1990-05-12 12 A
13 ASIT BHARGAV 8500 1989-04-29 12 B
14 ARPITA SHETTY 7200 1988-06-15 12 C
27 SUNILA DESAI 7000 1990-11-29 12 B

Select * From Student Where Fees Not Between 7000 And 9000;
Roll Name Fees Dob Class Sec
33 SUBHOJIT SAHA 6800 1989-01-31 12 C
10 SUSHIL JAIN 6900 1988-03-13 12 A

Select Name From Student Where Name Like 'A%';


Name
ASIT BHARGAV
ARPITA SHETTY

FAIPS, DPS Kuwait Page 9 of 18 © Bikram Ally


Database & SQL Class XII
Select Name From Student Where Name Like '%A';
Name
DHRUTI GUPTA
PUNEET ARORA
SUBHOJIT SAHA

Select Name From Student Where Name Like '%A%';


Name
MANISH SINGH
DHRUTI GUPTA
SUNIT KUMAR
PUNEET ARORA
SUBHOJIT SAHA
ASIT BHARGAV
SUSHIL JAIN
ARPITA SHETTY
SUNILA DESAI
Displays the names that contain at least one 'A'

SELECT with ORDER BY clause (DML)

Syntax:
i) Select * From TableName
Order By Col1 [Asc/Desc], Col2 [Asc/Desc], …;
ii) Select * From TableName
Where Condition Order By Col1 [Asc/Desc], Col2 [Asc/Desc], …;
iii) Select Col1, Col2, Col3, … From TableName
Where Condition Order By Col1 [Asc/Desc], Col2 [Asc/Desc], …;

Order By clause used with Select command displays the rows sorted on column (Columns). By
default the sorting is in ascending order. Order By will sort the rows logically, that is, rows will appear
on sorted order only on the screen but inside the table row position remains unaltered. By adding Desc
after the column name, rows appear in descending order. Asc after the column name will sort the rows in
ascending order. Since by default sorting is in ascending order, Asc is optional after the column name. If
Where Condition is omitted then all the rows of a table are sorted.

Examples:
Select * From Student Order By Roll;
Roll Name Fees Dob Class Sec
10 SUSHIL JAIN 6900 1988-03-13 12 A
13 ASIT BHARGAV 8500 1989-04-29 12 B
14 ARPITA SHETTY 7200 1988-06-15 12 C
16 PUNEET ARORA 7500 1990-05-12 12 A
17 DHRUTI GUPTA 8800 1988-02-28 12 C
21 MANISH SINGH 7800 1989-09-07 12 B
24 SUNIT KUMAR 7500 1989-08-24 12 A
27 SUNILA DESAI 7000 1990-11-29 12 B
33 SUBHOJIT SAHA 6800 1989-01-31 12 C
FAIPS, DPS Kuwait Page 10 of 18 © Bikram Ally
Database & SQL Class XII
Select * From Student Where Sec='B' Order By Name;
Roll Name Fees Dob Class Sec
13 ASIT BHARGAV 8500 1989-04-29 12 B
21 MANISH SINGH 7800 1989-09-07 12 B
27 SUNILA DESAI 7000 1990-11-29 12 B

Select * From Student Where Fees>=7800 Order By Fees Desc;


Roll Name Fees Dob Class Sec
17 DHRUTI GUPTA 8800 1988-02-28 12 C
13 ASIT BHARGAV 8500 1989-04-29 12 B
21 MANISH SINGH 7800 1989-09-07 12 B

Select Name, Sec, Dob, From Student Where Sec='A' Or Sec='B' Order By
Sec, Name;
Name Sec Dob
PUNEET ARORA A 1990-05-12
SUNIT KUMAR A 1989-08-24
SUSHIL JAIN A 1988-03-13
ASIT BHARGAV B 1989-04-29
MANISH SINGH B 1989-09-07
SUNILA DESAI B 1990-11-29

Select Name, Sec, Dob, From Student Where Sec='A' Or Sec='B' Order By
Sec, Name Desc;
Name Sec Dob
SUSHIL JAIN A 1988-03-13
SUNIT KUMAR A 1989-08-24
PUNEET ARORA A 1990-05-12
SUNILA DESAI B 1990-11-29
MANISH SINGH B 1989-09-07
ASIT BHARGAV B 1989-04-29

Select Name, Sec, Dob From Student Where Sec in ('A','B') Order By
Sec Desc, Name;
Name Sec Dob
ASIT BHARGAV B 1989-04-29
MANISH SINGH B 1989-09-07
SUNILA DESAI B 1990-11-29
PUNEET ARORA A 1990-05-12
SUNIT KUMAR A 1989-08-24
SUSHIL JAIN A 1988-03-13

SELECT with Aggregate functions or SQL functions or Group functions (DML)

COUNT() Counts the number of rows


SUM() Finds the sum of values in the column (only applicable for Numeric type)
AVG() Finds the average of values in the column (only applicable for Numeric type)
MAX() Finds the maximum value in the column
MIN() Finds the minimum value in the column
FAIPS, DPS Kuwait Page 11 of 18 © Bikram Ally
Database & SQL Class XII
Select Count(*) From Student;
Count(*)
9

Select Count(Rno) From Student;


Count(Rno)
9

Select Sum(Fees) From Student;


Sum(Fees)
68000.00

Select Avg(Fees) From Student;


Avg(Fees)
7555.56

Select Max(Fees), Min(Fees) From Student;


Max(Fees) Min(Fees)
8800 6800

Select Max(Dob), Min(Dob) From Student;


Max(Dob) Min(Dob)
1990-11-29 1988-02-27

Select Max(Name), Min(Name) From Student;


Max(Name) Min(Name)
SUSHIL JAIN AMOL SAHA

Select Sec From Student;


Sec
B
C
A
A
C
B
A
C
B

Select Distinct Sec From Student;


Sec
A
B
C

Select Count(Sec) From Student;


Count(Sec)
9
FAIPS, DPS Kuwait Page 12 of 18 © Bikram Ally
Database & SQL Class XII
Select Count(Distinct Sec) From Student;
Count(Distinct Sec)
3

SELECT with GROUP BY (DML)

Select Sec, Sum(Fees) From Student Group By Sec;


Sec Sum(Fees)
A 21900
B 23300
C 22800

Select Sec, Count(*) From Student Group By Sec;


Sec Count(*)
A 3
B 3
C 3

Select Sec, Max(Fees), Min(Fees) From Student Group By Sec;


Sec Max(Fees) Min(Fees)
A 7500 6900
B 8500 7000
C 8800 6800

Select Sec, Sum(Fees) From Student Group By Sec Having


Sum(Fees)>22000;
Sec Sum(Fees)
B 23300
C 22800

SQL Cartesian Product

Example of Cartesian Product:


Table: Student Table: Teacher
Roll SName Class TCode Class TName
1 AKASH XII 102 XII MS. RINEE
4 DIBAKAR XII 309 XII MR. TANMOY
7 ROHINI XI 270 XI MR. ALICE
10 RISHI XI
5 DEEPA XI

Select Roll, SName, Class, TCode, TName From Student, Teacher;


Roll SName Class TCode TName
1 AKASH XII 102 MS. RINEE
1 AKASH XII 309 MR. TANMOY
1 AKASH XII 270 MR. ALICE
4 DIBAKAR XII 102 MS. RINEE

FAIPS, DPS Kuwait Page 13 of 18 © Bikram Ally


Database & SQL Class XII
4 DIBAKAR XII 309 MR. TANMOY
4 DIBAKAR XII 270 MR. ALICE
7 ROHINI XI 102 MS. RINEE
7 ROHINI XI 309 MR. TANMOY
7 ROHINI XI 270 MR. ALICE
10 RISHI XI 102 MS. RINEE
10 RISHI XI 309 MR. TANMOY
10 RISHI XI 270 MR. ALICE
5 DEEPA XI 102 MS. RINEE
5 DEEPA XI 309 MR. TANMOY
5 DEEPA XI 270 MR. ALICE

Select Roll, SName, Class, TCode, TName From Student, Teacher


Where Student.Class=Teacher.Class;
Roll SName Class TCode TName
1 AKASH XII 102 MS. RINEE
1 AKASH XII 309 MR. TANMOY
4 DIBAKAR XII 102 MS. RINEE
4 DIBAKAR XII 309 MR. TANMOY
7 ROHINI XI 270 MR. ALICE
10 RISHI XI 270 MR. ALICE
5 DEEPA XI 270 MR. ALICE

Select Roll, SName, Class, Teacher From Student S, Teacher T


Where S.Class=T.Class AND Class="XII";
Roll SName Class TCode TName
1 AKASH XII 102 MS. RINEE
1 AKASH XII 309 MR. TANMOY
4 DIBAKAR XII 102 MS. RINEE
4 DIBAKAR XII 309 MR. TANMOY

SQL Union

Example of Union:
Table: Stu1 Table: Stu2
Adno Name Adno Name
20050014 FATIMA 20050017 REKHA
20050029 FARAZ 20050023 TUSHI
20050004 GAURI 20050008 ATUL

Select * from Stu1 Union Select * From Stu2;


Adno Name
20050014 FATIMA
20050029 FARAZ
20050004 GAURI
20050017 REKHA
20050023 TUSHI
20050008 ATUL

FAIPS, DPS Kuwait Page 14 of 18 © Bikram Ally


Database & SQL Class XII
Table: Employee
ENO NAME DESIG DOJ SAL MOB
1045 ADITYA JAIN MANAGER 2010-07-02 80000 99456972
1072 ALEX JOHN MANAGER 2007-04-09 75000 55012016
1032 RITA GUPTA MANAGER 2010-07-02 80000 66442288
1012 NITU BHAT DIRECTOR 2008-10-15 95000 55123765
1049 ALOKA SINGH DIRECTOR 2007-04-09 90000 64192837
1025 RITA GUPTA DIRECTOR 2008-10-15 95000 55876534
1017 ADITYA JAIN MANAGER 2008-09-03 75000 98283761
1030 PRADIP ROY DIRECTOR 2007-04-09 96000 67701246
We will use Employee table for the following:
1. Using expression in SELECT command in place of column name
SELECT ENO, NAME, DESIG, SAL, 0.25*BSAL FROM EMPLOYEE;
ENO NAME DESIG SAL 0.25*SAL
1045 ADITYA JAIN MANAGER 80000 20000
1072 ALEX JOHN MANAGER 75000 18750
1032 RITA GUPTA MANAGER 80000 20000
1012 NITU BHAT DIRECTOR 95000 23750
1049 ALOKA SINGH DIRECTOR 90000 22500
1025 RITA GUPTA DIRECTOR 95000 23750
1017 ADITYA JAIN MANAGER 75000 18750
1030 PRADIP ROY DIRECTOR 96000 24000

2. Using expression in SELECT command in place of column name


SELECT ENO, NAME, DESIG, SAL, 0.25*BSAL AS HRA FROM EMPLOYEE;
OR,
SELECT ENO, NAME, DESIG, SAL, 0.25*BSAL HRA FROM EMPLOYEE;
ENO NAME DESIG SAL HRA
1045 ADITYA JAIN MANAGER 80000 20000
1072 ALEX JOHN MANAGER 75000 18750
1032 RITA GUPTA MANAGER 80000 20000
1012 NITU BHAT DIRECTOR 95000 23750
1049 ALOKA SINGH DIRECTOR 90000 22500
1025 RITA GUPTA DIRECTOR 95000 23750
1017 ADITYA JAIN MANAGER 75000 18750
1030 PRADIP ROY DIRECTOR 96000 24000

3. Using constant in SELECT command in place of column name


SELECT NAME, 'IS A', DESIG FROM EMPLOYEE;
NAME IS A DESIG
ADITYA JAIN IS A MANAGER
ALEX JOHN IS A MANAGER
RITA GUPTA IS A MANAGER
NITU BHAT IS A DIRECTOR
ALOKA SINGH IS A DIRECTOR
RITA GUPTA IS A DIRECTOR
ADITYA JAIN IS A MANAGER
PRADIP ROY IS A DIRECTOR

FAIPS, DPS Kuwait Page 15 of 18 © Bikram Ally


Database & SQL Class XII
Table: Employee
ENO NAME DESIG DOJ SAL MOB
1045 ADITYA JAIN MANAGER 2010-07-02 80000 99456972
1072 ALEX JOHN NULL 2007-04-09 75000 55012016
1032 RITA GUPTA MANAGER 2010-07-02 80000 66442288
1012 NITU BHAT DIRECTOR 2008-10-15 95000 55123765
1049 ALOKA SINGH DIRECTOR 2007-04-09 90000 64192837
1025 RITA GUPTA DIRECTOR 2008-10-15 95000 55876534
1017 ADITYA JAIN MANAGER 2008-09-03 75000 98283761
1030 PRADIP ROY NULL 2007-04-09 96000 67701246
1020 AMIT KUMAR NULL 2010-07-02 77000 94627301
1. Using NULL in the query with WHERE clause
SELECT * FROM EMPLOYEE WHERE DESIG=NULL;
Syntactically correct SQL command but displays Empty Set

2. Using NULL in the query with WHERE clause


SELECT * FROM EMPLOYEE WHERE DESIG IS NULL;
ENO NAME DESIG DOJ SAL MOB
1072 ALEX JOHN NULL 2007-04-09 75000 55012016
1030 PRADIP ROY NULL 2007-04-09 96000 67701246
1020 AMIT KUMAR NULL 2010-07-02 77000 94627301

3. Using NULL in the query with WHERE clause


SELECT * FROM EMPLOYEE WHERE DESIG IS NOT NULL;
ENO NAME DESIG DOJ SAL MOB
1045 ADITYA JAIN MANAGER 2010-07-02 80000 99456972
1032 RITA GUPTA MANAGER 2010-07-02 80000 66442288
1012 NITU BHAT DIRECTOR 2008-10-15 95000 55123765
1049 ALOKA SINGH DIRECTOR 2007-04-09 90000 64192837
1025 RITA GUPTA DIRECTOR 2008-10-15 95000 55876534
1017 ADITYA JAIN MANAGER 2008-09-03 75000 98283761

4. Role of NULL in a query with DISTINCT


SELECT DISTINCT DESIG FROM EMPLOYEE;
DESIG
NULL
MANAGER
DIRECTOR

5. Role of NULL in a query with COUNT()


SELECT COUNT(DESIG) FROM EMPLOYEE;
COUNT(DESIG)
6

6. Role of NULL in a query with COUNT()


SELECT COUNT(DISTINCT DESIG) FROM EMPLOYEE;
COUNT(DISTINCT DESIG)
2
Note: Function COUNT() does not count NULL
FAIPS, DPS Kuwait Page 16 of 18 © Bikram Ally
Database & SQL Class XII
Cartesian Product and Union operation using SQL
Cartesian Product will create a new table by pairing every row of one table with every row of the other
table and Union will create a new table by getting rows from both the tables.
Table: STUDENT Table: TEACHER
ROLL SNAME CLA TNAME SUBJECT CLA
1 AKASH 12 MS. RITA GEOGRAPHY 11
5 DIPAK 11 MR. ANUP POLITICAL SC 12
24 RUPA 12
31 STUTI 11

To obtain the Cartesian Product of STUDENT and TEACHER, following SQL command is executed:
SELECT * FROM STUDENT, TEACHER;
ROLL SNAME CLA TNAME SUBJECT CLA
1 AKASH 12 MS. RITA GEOGRAPHY 11
1 AKASH 12 MR. ANUP POLITICAL SC 12
5 DIPAK 11 MS. RITA GEOGRAPHY 11
5 DIPAK 11 MR. ANUP POLITICAL SC 12
24 RUPA 12 MS. RITA GEOGRAPHY 11
24 RUPA 12 MR. ANUP POLITICAL SC 12
31 STUTI 11 MS. RITA GEOGRAPHY 11
31 STUTI 11 MR. ANUP POLITICAL SC 12
Fist row of STUDENT is paired with both rows of TEACHER, second row of row of STUDENT is
paired with both rows of TEACHER, third row of STUDENT is paired with both rows of TEACHER
and fourth row of STUDENT is paired with both rows of TEACHER. Instead of displaying all the
columns, we select columns to be displayed in the Cartesian Product. Edited SELECT command is given
below:
SELECT ROLL, SNAME, TNANE, CLA FROM STUDENT, TEACHER;
Executing this SQL query will trigger a syntax error because column CLA is common to both relations.
So, SQL is confused since it cannot decide which CLA from which table. To resolve the issue, one has to
specify the name of the common column to be preceded by the table name. SQL query SELECT * FROM
STUDENT, TEACHER; does not create any confusion since SQL has to select common column CLA
from both the tables. Corrected SELECT command is given below:
SELECT ROLL, SNAME, TNANE, STUDENT.CLA FROM STUDENT, TEACHER;
OR,
SELECT ROLL, SNAME, TNANE, TEACHER.CLA FROM STUDENT, TEACHER;
ROLL SNAME TNAME SUBJECT CLA
1 AKASH MS. RITA GEOGRAPHY 11
1 AKASH MR. ANUP POLITICAL SC 12
5 DIPAK MS. RITA GEOGRAPHY 11
5 DIPAK MR. ANUP POLITICAL SC 12
24 RUPA MS. RITA GEOGRAPHY 11
24 RUPA MR. ANUP POLITICAL SC 12
31 STUTI MS. RITA GEOGRAPHY 11
31 STUTI MR. ANUP POLITICAL SC 12

In the table TEACHER, MS. RITA teaches class 11 and MR. ANUP teaches class 12. While doing
Cartesian Product, students of class 11 and paired with teacher of class 12 and student of class 12 are
paired with teacher of class 11. So, we need to pair student class 11 with teacher of class 11 and student
of class 12 with teacher of class 12. This can be done through EQUI JOIN.
FAIPS, DPS Kuwait Page 17 of 18 © Bikram Ally
Database & SQL Class XII
SQL EQUI JOIN
A new table will be obtained by joining two tables using a common column.
SELECT ROLL, SNAME, TNANE, STUDENT.CLA FROM STUDENT, TEACHER
WHERE STUDENT.CLA=TEACHER.CLA;
ROLL SNAME TNAME SUBJECT CLA
1 AKASH MR. ANUP POLITICAL SC 12
5 DIPAK MS. RITA GEOGRAPHY 11
24 RUPA MR. ANUP POLITICAL SC 12
31 STUTI MS. RITA GEOGRAPHY 11

Instead of using complete table names in the query we can use an alias in place of table names. An alias
can be a single letter or string of letters.
SELECT ROLL, SNAME, TNANE, S.CLA FROM STUDENT S, TEACHER T
WHERE S.CLA=T.CLA;
OR,
SELECT ROLL, SNAME, TNANE, ST.CLA FROM STUDENT ST, TEACHER TE
WHERE ST.CLA=TE.CLA;
ROLL SNAME TNAME SUBJECT CLA
1 AKASH MR. ANUP POLITICAL SC 12
5 DIPAK MS. RITA GEOGRAPHY 11
24 RUPA MR. ANUP POLITICAL SC 12
31 STUTI MS. RITA GEOGRAPHY 11
Letter S(ST) is the alias for STUDENT and letter T(TE) is the alias for TEACHER.

SQL UNION
Table: CS12A Table: CS12B
NAME MARKS GRADE NAME MARKS GRADE
FATIMA 69.5 C1 ATUL 89.0 A2
FARUQ 78.0 B2 EKTA 67.5 C2
GAURI 95.5 A1 REKHA 88.5 B1

To obtain the UNION of two tables CS12A and CS12B, following SQL query is needed:
SELECT * FROM CS12A UNION SELECT * FROM CS12B;
NAME MARKS GRADE
FATIMA 69.5 C1
FARUQ 78.0 B2
GAURI 95.5 A1
REKHA 89.0 A2
EKTA 67.5 C2
ATUL 88.5 B1
OR,
SELECT * FROM CS12B UNION SELECT * FROM CS12A;
NAME MARKS GRADE
REKHA 89.0 A2
EKTA 67.5 C2
ATUL 88.5 B1
FATIMA 69.5 C1
FARUQ 78.0 B2
GAURI 95.5 A1
FAIPS, DPS Kuwait Page 18 of 18 © Bikram Ally

You might also like