Download Database Management System

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Oracle Database wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Concurrency control wikipedia , lookup

Functional Database Model wikipedia , lookup

SQL wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Ingres (database) wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

PL/SQL wikipedia , lookup

Clusterpoint wikipedia , lookup

ContactPoint wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Transcript
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
Designation
1045 JATIN KHANNA MANAGER FINANCE
1072 ALEX JOHN
MANAGER IT
1032 RACHITA GUPTA SENIOR MANAGER
1012 KALPANA BHAT DIRECTOR HRD
1049 RUPALI SINGH
GENERAL MANAGER
1025 AVEEK SHARMA DEPUTY MANAGER
1017 KAPIL GARG
DEPUTY DIRECTOR
DOJ
2010-07-02
2007-04-09
2009-05-04
2008-10-15
2009-06-10
2010-12-07
2008-09-03
Salary
90000
85000
78000
52000
70000
55000
74000
Mobile
99456972
55012016
66442288
55123765
64192837
54876534
98283761
Relational Database: It is a collection of 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 group 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 14
© Bikram Ally
Database & SQL
Relation: Employee
Eno
Name
1045 JATIN KHANNA
1072 ALEX JOHN
1032 RACHITA GUPTA
1012 KALPANA BHAT
1049 RUPALI SINGH
1025 AVEEK SHARMA
1017 KAPIL GARG
Class XII
Designation
MANAGER FINANCE
MANAGER IT
SENIOR MANAGER
DIRECTOR HRD
GENERAL MANAGER
DEPUTY MANAGER
DEPUTY DIRECTOR
DOJ
2010-07-02
2007-04-09
2009-05-04
2008-10-15
2009-06-10
2010-12-07
2008-09-03
Salary
90000
85000
78000
52000
70000
55000
74000
Mobile
99456972
55012016
66442288
55123765
64192837
54876534
98283761
Records
Rows
Tuples
Fields / Columns / Attributes
Degree
Number of Columns in a Table (Relation)
Cardinality
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 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 or group of attributes that contains distinct values which can identify a
tuple uniquely in a relation (table).
Non Key Attribute is an attribute which not a Candidate Key.
Relation: Employee
Eno
Name
1045 JATIN KHANNA
1072 ALEX JOHN
1032 RACHITA GUPTA
1012 KALPANA BHAT
1049 RUPALI SINGH
1025 AVEEK SHARMA
1017 KAPIL GARG
Primary Key
Designation
MANAGER FINANCE
MANAGER IT
SENIOR MANAGER
DIRECTOR HRD
GENERAL MANAGER
DEPUTY MANAGER
DEPUTY DIRECTOR
Candidate Keys
DOJ
2010-07-02
2007-04-09
2009-05-04
2008-10-15
2009-06-10
2010-12-07
2008-09-03
Salary
90000
85000
78000
52000
70000
55000
74000
Mobile
99456972
55012016
66442288
55123765
64192837
54876534
98283761
Alternate Key
In the Employee relation, Eno and Mobile are Key attributes. Eno is selected as Primary key, then
Mobile is the Alternate key. Attributes Name, Designation, DOJ and Salary are non-key attributes.
FAIPS, DPS Kuwait
Page 2 of 14
© 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): 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)
TName
ClassID
Subject
Roll SName Class
11
1
Akash
12
Ms. Rita
Home Sc
12
3
Dipak
11
Mr. Anup
History
5
Rupa
12
7
Stuti
11
Cartesian product of Student and Teacher: Student (R1) X Teacher (R2)
ClassID
Rn SName Class TName
Subject
11
1
Akash
12
Ms. Rita
Home Sc
12
1
Akash
12
Mr. Anup
History
11
3
Dipak
11
Ms. Rita
Home Sc
12
3
Dipak
11
Mr. Anup
History
11
5
Rupa
12
Ms. Rita
Home Sc
12
5
Rupa
12
Mr. Anup
History
11
7
Stuti
11
Ms. Rita
Home Sc
12
7
Stuti
11
Mr. Anup
History
4. Union (Binary Operator): 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)
Name
Marks Grade
Relation: CS12B (R2)
Name Marks
CS12A (R1) U CS12B (R2)
Grade
Name
Marks
Grade
Fatima
69.5
C1
Atul
89.0
A2
Fatima
69.5
C1
Faraz
78.0
B2
Esha
67.5
C2
Faraz
78.0
B2
Gauri
95.5
A1
Rekha
88.5
B1
Gauri
95.5
A1
Rekha
89.0
A2
Esha
67.5
C2
Atul
88.5
B1
FAIPS, DPS Kuwait
Page 3 of 14
© 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
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 (not deleting the column)
 The editing of values stored in a column or columns (not modifying the data type of column)
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 – sec is a name of a column that will store single character.
area char (20) – area is a name of a column whose length is exactly 20 character. The concept
of nul terminating string does exist for char type of data in SQL.
Examples of char type data: 'Anil', "Priya", '371', "28.5", ''
2. NUMERIC: Used to store numeric data. Any number, either integer or real number (floating point
value) can be a numeric type.
empno numeric(4,0) – empno is name of column that will store 4 digits integer value.
marks numeric(5,1) – marks is name of column that will store a floating value (5 significant
digits and 1 place after the decimal point).
Examples of numeric type data: 12, -30, 15.5, -9.5
3. DATE: Used to store date. A date type value is represented as YYYY-MM-DD. Date type constant
is enclosed with either single quote (') or pair of double quote ("). Both char type and date types are
enclosed with a pair either single quote (') or pair of double quote (") but char type and date are
treated differently. One cannot mix up char type with date type.
dob date – dob is name of column that will store a date in the YYYY-MM-DD format.
Examples of date type data: "1995-09-11", '2013-10-15'
Some useful SQL Commands of MySQL
1. When you start MySQL for the first time, you have to create a database. 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. For the practical we will create only one database. Every SQL command is terminated by a
semi-colon.
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 14
© 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 databasename
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;
6. When we need to transfer data from a formatted text file to a database table, we will use the
command LOAD DATA INFILE.
Example: mysql>LOAD DATA INFILE "EMP.TXT" INTO TABLE EMPLOYEE;
Data form the text file "EMP.TXT" will be copied to the table EMPLOYEE. Text file must be
located in the same folder where the table EMPLOYEE is located. If the text file is located
somewhere else, then after INFILE complete path along with the text file name is to be specified.
Example: mysql>LOAD DATA INFILE "G:\EMP.TXT" INTO TABLE EMPLOYEE;
Creating a new table in a database (DDL)
Syntax:
Create Table TableName (Col1 DataType, Col2 DataType, Col3 DataType,
…);
FAIPS, DPS Kuwait
Page 5 of 14
© Bikram Ally
Database & SQL
Class XII
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 5 columns (attributes). Column roll
will be integer type (maximum 2 digit integer), column name will be character type (maximum 25
characters and no terminating nul character), column fees will be floating point type (8 significant
digits, that is 6 places before decimal point and 2 places after the decimal point), column dob will be
date type.
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.
Syntax (i) is useful when values are available for all the columns of a row
Syntax (ii) is useful when values have to be entered in some selected columns of a row.
Examples:
i) Insert
Insert
ii) Insert
Insert
Into
Into
Into
Into
Student Values(21,'MANAS VERMA',7800,'1989-09-07');
Student Values(7,'DHRUTI GUPTA',8800,'1988-02-28');
Student(Roll,Name,Fees) Values(16,'PRATAP RAO',7500);
Student(Roll,Name,Fees) Values(2,'ANIL JAIN',8300);
Adding new column(s) in the table (DDL)
Syntax:
Alter Table TableName ADD Col DataType;
Alter Table command inserts (adds) a new column to an existing table.
Examples:
Modifying the existing content of the table (DML)
Syntax:
Update TableName Set Col1=Value1, Col2=Value2, … Where Condition;
Update command is used to makes changes in row (rows) of an existing table. If Where Condition
is missing then changes are made to all the rows of a table.
Example:
Update Student
Update Student
Update Student
Update Student
Update Student
Set
Set
Set
Set
Set
Class=12, Sec='A' Where Roll=16;
Fees=7800 Where Roll=16;
Dob='1989-04-18' Where Roll=31;
Sec='D' Where Sec='A';
Fees=1.1*Fees;
Deleting a row/rows from a table (DML)
Syntax:
Delete From TableName Where Condition;
FAIPS, DPS Kuwait
Page 6 of 14
© Bikram Ally
Database & SQL
Class XII
Delete From removes row (rows) from an existing table. If Where Condition is omitted then all
the rows of the table are deleted but structure of the table remains.
Examples:
Delete From
Delete From
Delete From
Delete From
Delete From
Student Where
Student Where
Student Where
Student Where
Student;
Roll=13;
Name='SUSHIL JAIN';
Sec!='C';
Roll=13 Or Roll=31;
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
21
MANISH SINGH
7800 1989-09-07
17
DHRUTI GUPTA
8800 1988-02-28
24
SUNIT KUMAR
7500 1989-08-24
16
PUNEET ARORA
7500 1990-05-12
33
SUBHOJIT SAHA 6800 1989-01-31
13
ASIT BHARGAV
8500 1989-04-29
10
SUSHIL JAIN
6900 1988-03-13
14
ARPITA SHETTY 7200 1988-06-15
27
SUNILA DESAI
7000 1990-11-29
CLASS
12
12
12
12
12
12
12
12
12
SEC
B
C
A
A
C
B
A
C
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
21
MANISH SINGH
7800
17
DHRUTI GUPTA
8800
24
SUNIT KUMAR
7500
16
PUNEET ARORA
7500
33
SUBHOJIT SAHA 6800
13
ASIT BHARGAV
8500
10
SUSHIL JAIN
6900
FAIPS, DPS Kuwait
DOB
1989-09-07
1988-02-28
1989-08-24
1990-05-12
1989-01-31
1989-04-29
1988-03-13
Page 7 of 14
CLASS
12
12
12
12
12
12
12
SEC
B
C
A
A
C
B
A
© Bikram Ally
Database & SQL
14
ARPITA SHETTY
27
SUNILA DESAI
7200
7000
Class XII
1988-06-15
1990-11-29
12
12
C
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
NAME
FEES
21
MANISH SINGH
7800
24
SUNIT KUMAR
7500
33
SUBHOJIT SAHA 6800
27
SUNILA DESAI
7000
Roll>20;
DOB
1989-09-07
1989-08-24
1989-01-31
1990-11-29
Select * From Student Where
ROLL
NAME
FEES
16
PUNEET ARORA
7500
10
SUSHIL JAIN
6900
17
DHRUTI GUPTA
8800
13
ASIT BHARGAV
8500
14
ARPITA SHETTY 7200
Roll>=10 And Roll<=20;
DOB
CLASS SEC
1990-05-12
12
A
1988-03-13
12
A
1988-02-28
12
C
1989-04-29
12
B
1988-06-15
12
C
CLASS
12
12
12
12
SEC
B
A
C
B
Select Roll, Name, Sec From Student Where Sec='A' Or Sec='B';
ROLL
NAME
SEC
21
MANISH SINGH
B
24
SUNIT KUMAR
A
FAIPS, DPS Kuwait
Page 8 of 14
© Bikram Ally
Database & SQL
16
PUNEET ARORA
13
ASIT BHARGAV
10
SUSHIL JAIN
27
SUNILA DESAI
Class XII
A
B
A
B
Select Roll, Name, Sec From Student Where Class!=12;
ROLL
NAME
SEC
No output on the screen since no row could be selected which satisfied the condition Class!=12
Select * From Student Where
ROLL
NAME
FEES
17
DHRUTI GUPTA
8800
24
SUNIT KUMAR
7500
16
PUNEET ARORA
7500
33
SUBHOJIT SAHA 6800
10
SUSHIL JAIN
6900
14
ARPITA SHETTY 7200
Sec In ('A', 'C');
DOB
CLASS
1988-02-28
12
1989-08-24
12
1990-05-12
12
1989-01-31
12
1988-03-13
12
1988-06-15
12
Select * From Student Where
ROLL
NAME
FEES
21
MANISH SINGH
7800
24
SUNIT KUMAR
7500
27
SUNILA DESAI
7000
Roll Between 21 And 30;
DOB
CLASS SEC
1989-09-07
12
B
1089-08-24
12
A
1990-11-29
12
B
Select * From Student Where
ROLL
NAME
FEES
21
MANISH SINGH
7800
17
DHRUTI GUPTA
8800
24
SUNIT KUMAR
7500
16
PUNEET ARORA
7500
13
ASIT BHARGAV
8500
14
ARPITA SHETTY 7200
27
SUNILA DESAI
7000
Fees Between 7000 And 9000;
DOB
CLASS SEC
1989-09-07
12
B
1988-02-28
12
C
1989-08-24
12
A
1990-05-12
12
A
1989-04-29
12
B
1988-06-15
12
C
1990-11-29
12
B
SEC
C
A
A
C
A
C
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
Select Name From Student Where Name Like '%A';
NAME
DHRUTI GUPTA
FAIPS, DPS Kuwait
Page 9 of 14
© Bikram Ally
Database & SQL
PUNEET ARORA
SUBHOJIT SAHA
Class XII
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
Outputs all the names since all the names contain at least one 'A'
SELECT with ORDER BY clause (DML)
Syntax:
i) Select * From TableName
Where Condition Order By Col1 [Asc/Desc], Col2 [Asc/Desc], …;
ii) 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
ROLL
NAME
FEES
10
SUSHIL JAIN
6900
13
ASIT BHARGAV
8500
14
ARPITA SHETTY 7200
16
PUNEET ARORA
7500
17
DHRUTI GUPTA
8800
21
MANISH SINGH
7800
24
SUNIT KUMAR
7500
27
SUNILA DESAI
7000
33
SUBHOJIT SAHA 6800
By Roll;
DOB
1988-03-13
1989-04-29
1988-06-15
1990-05-12
1988-02-28
1989-09-07
1989-08-24
1990-11-29
1989-01-31
CLASS
12
12
12
12
12
12
12
12
12
SEC
A
B
C
A
C
B
A
B
C
Select * From Student Where Sec='B' Order By Name;
ROLL
NAME
FEES
DOB
CLASS SEC
13
ASIT BHARGAV
8500 1989-04-29
12
B
FAIPS, DPS Kuwait
Page 10 of 14
© Bikram Ally
Database & SQL
21
MANISH SINGH
27
SUNILA DESAI
7800
7000
Select * From Student Where
ROLL
NAME
FEES
17
DHRUTI GUPTA
8800
13
ASIT BHARGAV
8500
21
MANISH SINGH
7800
Class XII
1989-09-07
1990-11-29
12
12
B
B
Fees>=7800 Order By Fees Desc;
DOB
CLASS SEC
1988-02-28
12
C
1989-04-29
12
B
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
Sec Desc, Name;
NAME
SEC
ASIT BHARGAV
B
MANISH SINGH
B
SUNILA DESAI
B
PUNEET ARORA
A
SUNIT KUMAR
A
SUSHIL JAIN
A
From Student Where Sec in ('A','B') Order By
DOB
1989-04-29
1989-09-07
1990-11-29
1990-05-12
1989-08-24
1988-03-13
SELECT with Aggregate functions or SQL function (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
Select Count(*) From Student
CNT
9
FAIPS, DPS Kuwait
Page 11 of 14
© Bikram Ally
Database & SQL
Class XII
Select Count(Rno) From Student
CNT
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
29/11/1990
28/02/1988
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 Count(Sec) From Student
CNT_SEC
9
Select Count(Distinct Sec) From Student
DCNT_SEC
3
Select Distinct Sec From Student
SEC
A
B
C
FAIPS, DPS Kuwait
Page 12 of 14
© Bikram Ally
Database & SQL
SELECT with GROUP BY (DML)
Class XII
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
CNT
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
Roll SName
1
Akash
4
Dibakar
7
Rohini
10
Rishi
5
Deepa
Class
XII
XII
XI
XI
XI
Table: Teacher
TCode Class
102
XII
309
XII
270
XI
TName
Ms. Rinee
Mr. Tanmoy
Mr. Alice
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
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
FAIPS, DPS Kuwait
Page 13 of 14
© Bikram Ally
Database & SQL
10
Rishi
10
Rishi
10
Rishi
5
Deepa
5
Deepa
5
Deepa
XI
XI
XI
XI
XI
XI
102
309
270
102
309
270
Class XII
Ms. Rinee
Mr. Tanmoy
Mr. Alice
Ms. Rinee
Mr. Tanmoy
Mr. Alice
Select Roll, SName, Class, Teacher From Student, Teacher
Where Student.Class=Teacher.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
ANo
R490
R876
R342
Name
Fatima
Faraz
Gauri
Table: Stu2
Ano
S123
S456
S315
Name
Rekha
Tushi
Atul
Select * from Stu1 Union Select * From Stu2
Ano Name
R490 Fatima
R876 Faraz
R342 Gauri
S123 Rekha
S456 Tushi
S315 Atul
FAIPS, DPS Kuwait
Page 14 of 14
© Bikram Ally