Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

SQL Notes 12bcs

Uploaded by

r62437766
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL Notes 12bcs

Uploaded by

r62437766
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Alter Table command


It is a DDL statement which can change the structure of a table. It can be used for the following purposes.
(i) For adding a new column:
ALTER TABLE tablename ADD columnname datatype(size);
ex: ALTER TABLE student ADD address varchar(20);
(ii) For deleting an existing column:
ALTER TABLE tablename DROP columnname;
(iii) For changing either the datatype or size of an existing column:
2. Drop table command
It is used for deleting a table from database
Syntax:
drop table tablename;
1. Insert into command( DML Command)
Syntax:
Insert into tablename(colname1,colname2,…..) values(val1,val2,…..);
2. SELECT COMMAND(DML COMMAND)
Syntax:
Select * from tablename;
Eg:
Select * from student;
3. Selecting particular columns
select docid,dname from doctor;
Note: Order of selection depends on order of display
4. Eliminating redundant data in a column using Distinct keyword
The distinct keyword eliminates duplicate rows from the results of a select command.
5. Selecting particular rows
The tuples/rows in a table can be filtered by specifying a condition through where clause of the select statement.
Eg:
selecting only students of class 12 from student table.
select * from student where class=12;
RELATIONAL OPERATORS:
SQL supports the following relational operators:
=,>,<,>=,<=,<>(not equal to)
6. LOGICAL OPERATORS:
SQL supports the following logical operators:
AND
OR
NOT
They are used to combine one or more relational operators.
Questions:
To list the students belonging to class 10 and 12 from student table.
select * from student where class=10 or class=12;
To list the details of doctors whose fees is more than 500 and belonging to cardiology department.
select * from doctor where fees>500 and department =’cardiology’;
7. Conditions based on a range(between operator)
The between operator defines a range of values that the column values must fall into to make condition true. Both upper
and lower limit are inclusive.
Eg:
To display the content of doctor table whose fees are in the range 500 to 800.
Query/command can be:
SELECT * from doctor where fees between 500 and 800;
To display the name of students whose marks are in the range 60 to 85;
8. Conditions based on a list.(IN operator)
To specify a list of values, IN operator is used. The IN operator selects values that match any value in a given list of values.
For eg to display the list of students belonging to class 9,10 and 11 in student table, the query can be written as:
SELECT * from student where class in(9,10,11);
9.
10. UPDATE(DML COMMAND)
Update command is used to change the content of a table. It belongs to DML category.
Syntax:
Update tablename set columnname=new value where condition;
eg:
To change the prize money of outdoor games by adding 1000 to it can be done as:
UPDATE Games set prizemoney=prizemoney+1000 where game=’outdoor’;

Order by clause:
Sorting Results of a Table/Relation - ORDER BY clause
Whenever a SELECT query is executed, the resulting rows emerge in a predefined order. You can sort the results or a query in
a specific order using ORDER BY clause.
The data in the table is not sorted ; only the results that appear on thescreen are sorted. The syntax of ORDER BY clause is :
SELECT <column name> [, <column name> , ... ]
FROM <table name> [WHERE <predicate> ] [ORDER BY <column name> ] ;
For example, to display the list of students(refer the above table) in the alphabetical order of their names, we can use the command :
SELECT * FROM Student ORDER BY name asc; (asc keyword is optional by default keyword for ascending order is asc)
Grouping Result-GROUP BY
The Group by Clause is used in SELECT statements to divide the table into groups. Grouping can be done by column name or
with aggregate functions in which case the aggregate produces a value for each group
For Eg: Consider the following student table defined below:
Admn Name Stream Optional Marks
STUDENT 1001 Shrishti Science CS 90
1002 Ashi Humanities Maths 80
1003 Aditya Commerce IP 60
1004 Ritu Raj Science IP 65
1005 Sonali Commerce Maths 60
1006 Saumya Science IP 65

From the above table if you want to display the total no:of students stream wise you may use the query:
Select Stream ,count(*) from student group by Stream;
The output will be:
Stream count(*)
Science 3
Humanities 1
Commerce 2
The condition in HAVING clause is applied on groups of records, not on individual records. Only the condition in
WHERE clause is applied on individual records.
SELECT stream, count(*) FROM student GROUP BY stream HAVING count(*) < 3 ;

You might also like