You are on page 1of 6

SQL SQL stand for structured query language, a language to manipulate database.

Now a days It is a universal database language to create , manipulate database across plate forms. The data types supported by the standard SQL are as follows S.No ata Type !eaning $ %har It can store information containing alphabets and numeric or alphanumeric 2 .ar%harNumber It can store information containing alphabets and numeric or alphanumeric. /ut these data types when used can increase it 0s si1e when required It can store numeric values which can have fractional part or without fractional part "#ample Char (30) can store &ram' , &ram(i))*' or &+), b surya Nagar Varchar2 (30) can store &ram' , &ram(i))*' or &+), b surya Nagar 3$4 Number(2) mean it can store a numeric values between )) and 55 3-4 number(8,2) means it can store values whose after decimal points values can go upto two places Date can store any valid date

ate

It is used to store date type information

Operator 7 SQL supports different types of operators some of them is as follows $. Arithmetic operator "#ample are 7 8 , , , 9 and : -. Logica operator e#amples are 7 ; , < , =, ;= , <=, >= 3 not equal to 4 2. !e ationa Operator "#amples area 7 ?N , @A , N@T DDL 7 L stand for data definition language . it is basically responsible to define the structure of any database table. The commands falls under this category is as follows $. %reate Table -. ?lter Table 2. rop Table D"L 7 !L stand for data manipulation language. This is basically responsible for managing the records3 row:tuple4 of any table. The main command falls under this category are $. Insert -. elete 2. Bpdate 6. Select C. %reate view D. rop .iew Create #ab e 7 This L command is used to create a new table into any e#isting database . The synta# of this command is as follows Synta# %reate table <tblname; 3 columnEname datatype si1e constraints , columnEname datatype si1e constraints , columnEname datatype si1e constraints , ,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,, 4F

$%amp e & Sql>Create table student ( admno number(4) , roll number(2), Name char(30), fname char(30), DOB date ddress char(!0) )" To see the structure of the above defined table is as follows Sql; desc student F AL#$! #A'L$ 7 This L command is used to add : modify a column in any e#isting table of a database The synta# of this command is as follows Synta# ?lter table <tblname; ? :!@ IGH 3 columnEname datatype si1e constraints , columnEname datatype si1e constraints , columnEname datatype si1e constraints , ,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,, 4F "#ample #a() & To add a new column & Ihone & having datatype char and si1e $- in a table student Sql > alter table student DD ( #hone char ($2)) " TasJ 7 To change the datatype of admno into char and increase the si1e as C Sql ; alter table student !@ IGH 3 admno char3C44F Drop #ab e * This L command is used to remove a table from any e#isting database. This synta# of this command is as follows S+nta% rop table < table name; "#ample Sql; drop table student F

,NS$!# CO""AND 7 This !L command is used to add a new row3 record: tuple 4 in any e#isting database table . The synta# is as follows Synta# insert into < table name ; values 3 value$, value-, value 2,.................value N 4F "#ample TasJ 7 ?dd a new row in a table student 3 which we have created earlier4 Sql ; Insert into student values 3 0$-26?K,K$-,K 0(oshin gulatiK,KKabcdK, 05,apr,-))+K,Kb,$C- surya nagarK,K)$-),-D-D$2-K Note & A -a+( put char t+pe .a ue( an/ /ate t+pe .a ue( in (ing e in.erte/ comma S$L$C# CO""AND & This !L command is used to retrieve information from any table 3s4 to display: list: report on the screen, but this command can not any how update: modify any table. The synta# of this command is as follows Synta# Select L 9 : column list M from < table name; L where < condition ; M L order by < col name..; Lasc:descM M L group by < col name;M "#amples TasJ 7 To display all the records of table "!I Sql > select % from &'("

TasJ 7 To display only the employee no and employee name Sql > select em#no , ename from &'(" TasJ 7 to display employee name in first column and employee number on second column Sql> select ename, em#no from &'(" 0here c au(e & it is used to restrict the no no of rows from being displayed on the screen TasJ 7 display all the employees whose salary is greater than $C)) Sql ; select 9 from emp where sal ; $C)) TasJ 7 display all the employees information whose salary is between $C)) and -C)) Sql ; select 9 from emp where sal ; =C)) and sal <=-C)) TasJ 7 isplay all the information of clerJs Sql ; select 9 from emp where (ob =K%L"ANKF TasJ 7display the information of all the employees whose (ob is 0salesman 0 or 0%L"?NK Sql; select 9 from emp where (ob =KS?L"S!?N 0 @A (ob =K%L"ANK TasJ 7 isplay the information of all those employee whose (ob is not president Sql; select 9 from emp where (ob >=KIA"SI "NTK @A Sql; select 9 from emp where N@T (ob=KIA"SI "NTK Ca cu ate/ 1ie / & TasJ 7 isplay employee number, name and salary earned by each employee in a year from the table "!I 3 Suppose salary is paid in every month4 Sql ; select empno,ename , salary 9$- from "!IF NO#$ 7 In the above e#ample salary 9 $- is a calculated field. Comparing -ith N2LL .a ue( TasJ 7 isplay employees information who earns NBLL commission Sql ; select 9 from "!I Ohere sal is NBLL N@T" 7 o not use equal sign to compare NBLL values.

Li)e C au(e 333334attern "atching & This clause is used to display only those records which contains a single or multiple char from a given set of values . TasJ 7 isplay all the names starting with alphabet 0?K from table "!I. Sql ; select ename from emp where ename liJe 0?PF TasJ 7 display all the name starting with alphabet 0?K and must contains only C letters Sql; select ename from emp where ename liJe 0?E E E EKF Note & 5 333 6 rep ace a the remaining char(() 7 (un/er Score) 33333 6 !ep ace on + a (ing e char at a time

!emo.ing Dup icate !o-( ( Di(tinct 8e+-or/) TasJ 7 isplay ifferent (obs available in Table "!I Sql ; select distinct 3 (ob 4 from "!IF

Or/er '+ c au(e 7 This select clause is used to display the retrieved data in an arrange format

TasJ 7 display employee table information according to the name in asceding order from the table "!I Sql ; select 9 from emp order by name asc TasJ 7 isplay only employee names in descending order from the table "!I Sql ; display ename from emp order by ename desc TasJ 7 ispay the information of only clerJ in ascending order according to their name from table "!I Sql > select ename from &'( )here *ob +,cler-, Order b. ename asc" NO#$ & A(c i( not compu (or+ but /e(c i( compu (or+ 9roup '+ c au(e & 3 This select clause is basically used to find out the result when based on a group of values TasJ 7 Gind out the total no of employee in each (obs from "!I table Sql ; select (ob , count3(ob4 from "!I Qroup by (obF TasJ 7 Gind out average salary of clerJs from table "!I Sql ; select (ob , avg3sal4 from "!I Ohere (ob =K%L"ANF Qroup /y (ob

4ractice
Q:; #ab e & Schoo 'u( Atno ?reaEovered %apacity Noofstudents istance Transporter %harges $ .asant Jun( $)) $-) $) Shivamtravels $))))) Rau1 Nhas +) +) $) ?nand travels +C))) 2 Iitampura D) CC 2) ?nand travels D)))) 6 Aohini $)) 5) 2C ?nand travels $))))) C Hamuna .ihar C) D) -) /halla %o. CC))) D Nrishna Nagar *) +) 2) Hadav %o. +)))) * .asundhara $)) $$) -) Hadav %o. $))))) + Iaschim .ihar 6) 6) -) Speed travels CC))) 5 SaJet $-) $-) $) Speed travels $))))) $) SanJ Iuri $)) $)) -) Nisan Tours 5C))) 3b4 To show all information of students where capacity is more than the no of student in order of rtno. 3c4 To show areaEcovered for buses covering more than -) Jm., but charges less then +)))). 3d4 To show transporter wise total no. of students traveling. 3e4 To show rtno, areaEcovered and average cost per student for all routes where average cost per student is , charges:noofstudents. 3f4 ?dd a new record with following data7 3$$, & !oti bagh',2C,2-,$),' Jisan tours &, 2C)))4 3g4 Qive the output considering the original relation as given7 3i4 select sum3distance4 from schoolbus where transporter= & Hadav travels'F 3ii4 select min3noofstudents4 from schoolbusF 3iii4 select avg3charges4 from schoolbus where transporter= & ?nand travels'F 3iv4 select distinct transporter from schoolbusF Q2; #A'L$ & 9!AD2A#$
S;NO $ NA"$ N?A?N IO?N?A S#,4$ND 6)) 6C) S2'<$C# IRHSI%S %@!I. Sc. AV$!A9$ D+ D+ D,V; I I

2 6 C D * + 5 $)

I.H? A"NR? ?ASBN S?/IN? S@RN A@/"AT AB/IN? .IN?S

2)) 2C) C)) 6)) -C) 6C) C)) 6))

%R"!ISTAH IRHSI%S !?TRS %"R!ISTAH IRHSI%S !?TRS %@!I. Sc. !?TRS

DD2 *) CC D6 D+ DC*

I I I II I I I II

(a) (b) (c) (/) (e)

(1)

List the names of those students who have obtained I. $ sorted by N?!". isplay a report, listing N?!", STII"N , SB/S"%T and amount of stipend received in a year assuming that the STII"N is paid every month. To count the number of students who are either IRHSI%S or %@!IBT"A S% graduates. To insert a new row in the QA? B?T" table7 $$,'N?S@L', 2)), &computer sc', *C, $ Qive the output of following sql statement based on table QA? B?T"7 3i4 Select !IN3?."A?Q"4 from QA? B?T" where SB/S"%T='IRHSI%S'F 3ii4 Select SB!3STII"N 4 from QA? B?T" OR"A" div=-F 3iii4 Select ?.Q3STII"N 4 from QA? B?T" where ?."A?Q";=DCF 3iv4 Select %@BNT3distinct SB/ S"%T4 from QA? B?T"F ?ssume that there is one more table QBI " in the database as shown below7 #ab e& 92,D$ "A,NA!$A IRHSI%S %@!IBT"A S% %R"!ISTAH !?TR"!?TI%S ADV,SO! .IN@ ?L@N A?S?N !?R"SR

g) Ohat will be the output of the following query7 S"L"%T N?!", ? .IS@A GA@! QA? B?T",QBI " OR"A" SB/S"%T= !?IN?A"?F Q3; Orite SQL command for 3i4 to 3vii4 on the basis of the table SI@ATS #ab e& S4O!#S Student N@ $) $$ $$2 $6 $C %lass * + * * 5 $) Name Sammer Su(it Namal .enna ?rchana ?rpit Qame$ %ricJet Tennis Swimming Tennis /asJetball %ricJet Qrade / ? / % ? ? QameSwimming SJating Gootball Tennis %ricJet ?theletics Qrade? % / ? ? %

3a4 3b4 3c4

isplay the names of the students who have grade 0%K in either Qame$ or Qame- or both. isplay the number of students getting grade 0?K in %ricJet. isplay the names of the students who have same game for both Qame$ and Qame-.

3d4 isplay the games taJen up by the students, whose name starts with 0?K. 3e4 ?ssign a value -)) for !arJs for all those who are getting grade 0/K or grade 0?K in both Qame$ and Qame-. 3f4 ?rrange the whole table in the alphabetical order of Name. 3g4 ?dd a new column named 0!arJsK.

You might also like