Structured Query Language (1)
Structured Query Language (1)
Language
The Structured Query Language(SQL) is a language that enables you
to create and operate on relational databases,which are sets of
related information stored in tables.
The Sequel language has evolved since then and its name is changed
to SQL. In 1986, The American National Standards Institute (ANSI)
published an SQL standard that was updated again in 1992.
Processing Capabilities of SQL
Example:
Create Table student(rollno integer,name char(20),stream char(30));
Default constraint –when the user does not define a value, it takes the
default value.
Check constraint – limits the values that can be inserted into a column
of a table.
To display the roll number name and stream for students who have
opted for ‘Science ‘ stream
Select name from Student where stream = “Commerce” and fees >4000;
Name
Anita Mehta
To display the names and roll numbers of students who have opted for
stream other than humanities.
Select rollno,name from student where not stream = “Humanities’;
Query based on range
To display the name,stream and fees of those students whose fees is
more than 2000 and less than4000
Or
Select name,stream,fees where fees>30 and fees <50;
Query based on a list:
To display the roll numbers,names of students who belong to house
‘Ekta’,’Shakti’,’Pragati’
Select rollno,name from student where house
in(‘Ekta’,’Shakti’,’Pragati’);
Table : Employee
ORDER BY CLAUSE
The ORDER BY clause allows sorting of query results by one or more
columns.
The sorting can be done either in ascending or descending order,the
default order is ascending.
Syntax:
SELECT <column name> from table name where <condition/predicate>
order by column name;
Aggregate functions:
Example :
To calculate the number of employees in each
department for employees .
Select count(*) from employee group by department;
Output
Department Count(*)
HR 1
Accounts 2
Sales 2
Finance 2
Employee
Employee Empname
Empname Department
Department Designation
Designation Salary
Salary
number
number
11 Ashish
AshishVerma
Verma HR
HR Manager
Manager 20000
20000
22 Deepa
Deepa kapoor
kapoor Accounts
Accounts Assistant
Assistant 7000
7000
33 Neha
Neha Sales
Sales Receptionist
Receptionist 5500
5500
44 Gaurav
Gaurav Sales
Sales Manager
Manager 3500
35000
Mehta
Mehta
55 Ranjan
Ranjan Finance
Finance Assistant
Assistant 10000
10000
Manager
Manager
66 Rajesh
Rajesh Accounts
Accounts CA
CA 3500
350000
77 Dinesh
Dinesh Finance
Finance Assistant
Assistant 6000
6000
Group by using the aggregate functions
Output
Department sum
HR 20000
Accounts 10500
Finance 9000
Sales 16000
Having Clause:
The Having clause is used to place conditions in the group by clause
The Create View command
A View is a virtual table. It is created from another existing table called the
base table.
Alter table
To add the column in the table:
Syntax:
Alter table<table name>ADD <column name><data type><size>;
Example:
Alter Table student Add Busroute integer;
Will add a new column name busroute to the table.
Drop table
This command is used to delete the entire table
Syntax
DROP TABLE <table name>
Example :
To delete the student table
DROP TABLE student;
DROP VIEW
To delete a view
DROP view stu;
DROP VIEW does not delete the base table
UPDATE command
To modify the tuples in the table
Syntax: Update student set fees =fees +200;
Increases the fees of all the students by `200 .
Example:
To calculate the average salary of employees for ‘sales’ department.
The Delete command is used to delete a row or tuple from the table.
Syntax:
Delete From <tablename> [where <condition / predicate];
Whenever the query on the two tables is done there should be a common
primary key so that the data can be related.
Example :
Table 1 : Employee
Employee
Employee Empname
Empname Department
Department Designation
Designation Salary
Salary
number
number
11 Ashish
AshishVerma
Verma HR
HR Manager
Manager 20000
20000
22 Deepa
Deepa kapoor
kapoor Accounts
Accounts Assistant
Assistant 7000
7000
33 Neha
Neha Sales
Sales Receptionist
Receptionist 5500
5500
44 Gaurav
Gaurav Sales
Sales Manager
Manager 35000
35000
Mehta
Mehta
55 Ranjan
Ranjan Finance
Finance Assistant
Assistant 10000
10000
Manager
Manager
66 Rajesh
Rajesh Accounts
Accounts CA
CA 350000
350000
77 Dinesh
Dinesh Finance
Finance Assistant
Assistant 6000
6000
Table 2 : Empinfo
2 5 12 B
3 8 20 A
5 10 15 B
6 3 18 A
7 2 20 C