0% found this document useful (0 votes)
15 views2 pages

SQL3

The document discusses SQL commands for creating, modifying, querying and manipulating tables. It provides the syntax for common statements like CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT. It also describes logical operators, aggregate functions like COUNT, SUM, AVG, MIN, MAX and clauses like GROUP BY, HAVING, ORDER BY. An example table called Student is given with sample data and SQL queries are written and output is provided for the queries on this table.

Uploaded by

blestrbhakt
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)
15 views2 pages

SQL3

The document discusses SQL commands for creating, modifying, querying and manipulating tables. It provides the syntax for common statements like CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT. It also describes logical operators, aggregate functions like COUNT, SUM, AVG, MIN, MAX and clauses like GROUP BY, HAVING, ORDER BY. An example table called Student is given with sample data and SQL queries are written and output is provided for the queries on this table.

Uploaded by

blestrbhakt
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/ 2

□ Syntax of the Statemen s :

1. Create:

Create table <table name> (<datatype><name of the field ><size> .. .. . .. ) ;


2. Alter:
Alter table <table name> add .
Alter table <table name> mod~~- .· .· .· .· _' ;
Alter table <table name> drop ........ ;
3. Drop:
Drop table <table name>.
'
4. Insert:
Insert into <table name> values( .. . ... ) ;
S. Update:
Update <table name> set dob = '2001/11/05'; //All dob will become 2001/11/05
► 'Where' clause is used to set criteria on records.
Update <table name> set dob = '2001/11/05' where <condition>;
6. Delete:
Delete from <table name> where <condition> ;
7. Select:
Select * from <table name> where <condition> ·
'
'*' is used to display all the fields
Logical operators : AND OR NOT
Other Important Operators : IN LIKE

► Some Important Group functions: Which work on number of rows.


sum ( ) calculates sum of a particular field, i.e., all the values present in a field.
avg ( ) finds out the average of values.
min () picks up the minimum value in the given range.
max () picks up the maximum value in the given range.
count () counts the values in a field.

► 'Order By' clause


F oreg.: select name from <table name> order by <field name> asc ;
asc = ascending order desc = descending order
If nothing is mentioned, Order wi II be in Ascending Order!!

► ' Having' Clause : The 'Having' Clause places c~nd,itions o,n grou~~ in
contrast ~o ' Where' clause
that places conditions on individual rows . Whtie Where condmons cannot include aggregate
functions, , Having' conditions can do so .
ss) sum(gross) from employee Group By grade Having grade= 'E4';
For eg.: Select avg ( gro ,
. (*) from emp Group By job Having count(*)<3;
Select Job, count
Relation Student :
Date of adm Fee Sex
No. Name Age Department
120 M
computer 10,0:1/97
1. Pankaj 24
200 F
History 240~8
2. Shalinl 21
300 M
Hindi 12/11/96
3. Sanjay 22
400 F
History Ol,07/99
4. Sudha 25
250 M
Hindi 0'$)9/97
5. Rakesh 22
300 M
30 History 27,0&98
6. Sha keel
210 M
7. Surya 34 Computer 2'$J2/.J7
200 F
8. Shikha 23 Hindi 31,07/97

Write SQL commands for (a) to (f) and write output for-(g).
(a) To show all information about the students of History department.

(b) To list the names of female students who are in Hindi department.

(c) To list names of all students with their date of admission in ascending order.

(d) To display student's Name, Fee, Age for Male Students only.

(e) To count the number of students with Age< 23.

(f) To insert a new row in the 'Student' table with the following data :
9, "Zaheer", 36, "Computer", {12/03/97}, 230, "M"
(g) Give the output of the following SQL statements:
(i) Select COUNT (distinct Department) from Student;
(ii) Select MAX(Age) from Student where Sex= "F"·, ·
(iii) Select AVG(Fee) from Student where Dateofadm < {O 1/01/98};
(iv) Select SUM(Fee) from Student where Dateofadm < {01/01/98};
SOLUTION:

(a) Select• from Student where Department= "History";

(b) Select Name from Student where Sex= "F" dD


an epartment = uHindi"·
(c) Select Name from Student order by Dateofadm· ,
'
(d) Select Name, -Fee, Age from Student wh S
ere ex= "M"·,
(e) Select COUNT{*) from Student whe A
rege<23;
(f) Insert into Student Values ( 9 "Zah ,, "
, eer , 36 Comput ,, {
(g) (i) 3 ( ..) , er , 12/03/97}, 230, "M");
II 25
(iii) 236 (iv) 1080

You might also like