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

Single Line Andgroup Function: Student

This document contains SQL statements to: 1. Create databases, tables, insert data, update data, alter tables, drop tables, and perform various queries using functions, joins, and aggregate operations. 2. Demonstrate transaction management commands like begin transaction, save transaction, rollback, and commit. 3. Show different types of joins, set operations, and subqueries. 4. Create views, use cursors, and define functions.

Uploaded by

indrajitmetya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Single Line Andgroup Function: Student

This document contains SQL statements to: 1. Create databases, tables, insert data, update data, alter tables, drop tables, and perform various queries using functions, joins, and aggregate operations. 2. Demonstrate transaction management commands like begin transaction, save transaction, rollback, and commit. 3. Show different types of joins, set operations, and subqueries. 4. Create views, use cursors, and define functions.

Uploaded by

indrajitmetya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

SINGLE LINE ANDGROUP FUNCTION

/*TO CREATE DATABASE*/ create database student /*TO CREATE TABLE*/ create table stud(sid int primary key,sname varchar(20) not null,staffname varchar(25) not null,maths int,science int,accounts int,grade varchar(3)); /*TO INSERT insert into insert into insert into insert into insert into THE VALUES TO TABLE*/ stud values(1,'SREERAM','KUMARASAMY',98,43,66,'C') stud values(2,'ANITHA','MATHI',88,48,67,'B') stud values(3,'RAJESH','SIVAKUMAR',89,67,86,'B+') stud values(4,'DINESH','BALA',96,93,89,'A') stud values(5,'RAJESH','SIVA',89,67,86,'B')

/*TO VIEW THE TABLE*/ select * from stud

/*UPDATE THE DETAILS*/ update stud set grade='A' where sname='RAJESH' update stud set maths=99 where sid=103

/*TO CREATE NEW COLUMN*/ alter table stnd add game points int

/*TO UPDATE update stnd update stnd update stnd update stnd

VALUES INTO THE COLUMN*/ set gamepoints=78 where SID=101 set gamepoints=67 where SID=102 set gamepoints=45 where SID=103 set gamepoints=56 where SID=104

/*TO VIEW THE ELEMENTS WITHOUT DUPLICATION */ select distinct sname from stud

/*TO DISPLAY SPECIFIED NUMBER OF RECORDS*/ select top 3 *from stnd

select top 50 percent *from stud

/*TO PERFORM ARITHMETIC OPERATION IN SPECIFIED FIELD*/ select science-2 as science from stud

/*TO CHANGE THE COLUMN NAME TEMPORARILY*/ select sid AS stid from stud

/*TO TRUNCATE THE RECORDS*/ truncate table stud

/*TO DISPLAY FIELDS STARTS WITH SPECIFIED CHARACTER*/ select sid,sname,grade from stud where sname like'A%'

/*DISPLAY THE FIELDS END WITH SPECIFIED CHARACTER*/ select sid,sname from stud where sname like'%h'

/*TO DISPLAY THE FIELDS BASED ON SECOND AND THIRD CHARACTER*/ select sname,grade FROM stud where sname like'_N%'

select sid,grade FROM stud where sname like'__J%'

/*TO DISPLAY THE DETAILS BASED ON ORDER*/ select *from stud order by SID DESC

select sname,grade from stud order by sname ASC

/*TO DISPLAY USING BETWEEN OPERATION*/ select sid,maths,science,accounts,grade from stud where science between 50 and 80

/*TO display fields two conditions*/ select sid,sname,grade from stud where sname like'r%'and grade like'%a'

/*TO PERFORM CONDITIONAL OPERATION*/ select sname,sid,science from stud where accounts<50

select sname,sid,science from stud where accounts>50

select sid,sname,fname,grade from stud where science>85 and science<95

/*DROP THE COLUMN*/ alter table stud drop column gamepoints

/*TO DROP A RECORDS*/ delete from stud where sid=104

/*RENAME THE TABLE*/ exec sp_rename 'stud','stddd' /*RENAME A COLUMN*/ exec sp_rename 'stddd.[sid]','rno','column'

/*SINGLE LINE FUNCTION*/ select lower('GOWTHA') as lower

select upper('abdul') as upper

select substring ('saravanan',2,6) as substring

select len('sathya')

select replace ('shanmugam','a','s') as replace

select ltrim('gow*****') as name

select rtrim('******saran') as name

select left('karthik',3) as name;

select right('karthik',3) as name;

select reverse ('santhosh')

select charindex('l','abdul')as index1

select SNAME+','+space(2)+'ARAJAN' from stnd2 where SNAME='SREERAM'

/*NUMERIC OPERATION*/ select round(129.647363,6),round(1234.89855,3)

select power(5,3)

select ABS(-546)

select sqrt(49)

select isnumeric('s')

select isnumeric(1)

select ceiling(123.45)

select floor(-132.767),floor(45.89),floor(132.78)

/*Date function*/ select getdate()as date

select day('6/12/2011')as day ,month('8/12/2011') as month,year('8/12/2011') as Year

select datename(month,getdate())as monthname

select datepart(month,getdate())as monthpart

select dateadd(day,10,'2/2/2011')as addday

select datediff(day,'1/4/2011','1/5/2011')as differentday

select isdate ('9/12/2011')

select isdate ('fgg')

/*USE AGGREGATE FUNCTION*/


select min(maths)as maths from stddd

select max(maths)as maths from stddd

select sum(science)as sience from stddd

select count(rno)as rno from stddd

select count(distinct grade)as grade from stddd

select avg (accounts)from stddd

/*GROUP FUNCTION*/ select sname,sum(maths) from stnd2 group by sname having sum(maths)>50

/*DROP THE TABLE*/ drop table stddd

JOIN AND SET OPERATION


/*TO CREATE DATABASE*/ create database employee /*TO CREATE TABLE1*/ create table emp1(ename varchar(20),city varchar(20),phno int) /*TO INSERT insert into insert into insert into insert into VALUES INTO emp1 values emp1 values emp1 values emp1 values TABLE*/ ('arun','selam',245436) ('krishna','chennai',287680) ('rahul','erode',213465) ('karthik','madurai',209867)

/*TO CREATE TABLE2*/ create table emp2(ename varchar(20),dest varchar(20),salary int) /*TO INSERT insert into insert into insert into insert into VALUES INTO emp2 values emp2 values emp2 values emp2 values TABLE*/ ('karthik','managing directer',15000) ('saran','manager',10000) ('rahul','asst manager',8500) ('krishna','prj manager',5600)

/*JOIN OPERATIONS*/ /*INNER JOIN*/ select emp1.ename,emp1.city from emp1 inner join emp2 emp1.ename= emp2.ename on

/*LEFT OUTER JOIN*/ select emp1.ename,emp1.city,emp2.salary from emp1 left outer join emp2 on emp1.ename= emp2.ename

/*RIGHT OUTER JOIN*/ select emp1.city,emp2.dest from emp1 right outer join emp2 emp1.ename= emp2.ename

on

/*FULL JOIN*/ select * from emp1 full join emp2

on emp1.ename= emp2.ename

/*SET OPERATION*/ /* TO CREATE TABLE3 */ create table emp3 (eid int primary key,ename varchar(20),place varchar(20)) /*TO INSERT insert into insert into insert into insert into VALUES INTO TABLE*/ emp3 values(1,'karthik','chennai') emp3 values(2,'ram','chennai') emp3 values(3,'deva','coimbatore') emp3 values(4,'bala','selem')

/* TO CREATE TABLE4 */ create table emp4 (eid int,ename varchar(20),place varchar(20)foreign key(eid) references emp3,phno int) /*TO INSERT insert into insert into insert into insert into VALUES INTO emp4 values emp4 values emp4 values emp4 values TABLE*/ (1,'arun','cbe',3434) (2,'ram','chennai',287680) (3,'rahul','erode',213465) (4,'karthik','chennai',209867)

/*UNION OPERATION*/ (select ename,place from emp3) union (select ename,place from emp4)

/*UNION ALL OPERATION*/ (select ename,place from from emp4)

emp3)

union

all

(select

ename,place

/*INTERSECTION OPERATION */ (select ename from emp3) intersect (select ename from emp4)

(select distinct ename from emp3) intersect (select ename from emp4)

/*EXCEPT OPERATION*/ (select ename from emp3) except (select ename from emp4)

/*TO VIEW TABLE1*/ select * from emp

2.TCL COMMANDS
/*TO CREATE DATABASE*/ create database stud /*TO CTREATE TABLE*/ create table std(sno int,sname varchar(20),phno int) /*TO INSERT VALUES INTO TABLE*/ insert into std values(1,'arun',246546) insert into std values(2,'raj',256734)

/*TO BEGIN TRANSACTION*/ begin transaction stu /*TO SAVE THE TRANSACTION*/ save transaction stu
/*TO CREATE NEW COLUMN*/ alter table std add total int

/*TO UPDATE VALUES INTO THE COLUMN*/ update std set total=780 where sno=1 update std set total=670 where sno=2 update std set total=450 where sno=3 update std set total=560 where sno=4

update std set phno=678575 where sno =1 update std set phno=565556 where sno =2

/*TO CREATE NEW COLUMN*/ alter table stud add total int /*TO UPDATE update stud update stud update stud update stud VALUES INTO THE COLUMN*/ set total=780 where sid=1 set total=670 where sid=2 set total=450 where sid=3 set total=560 where sid=4

/* TO ROLLBACK TO PREVIOUS TRANSACTION*/ rollback transaction stu

/* TO SAVE CHANGES PERMONENTLY*/ commit transaction stu /* TO VIEW THE TABLE*/ select * from std

3.JOIN,SET OPERATION and sub quries


/*TO CREATE DATABASE*/ create database employee /*TO CREATE TABLE1*/ create table emp1(ename varchar(20),city varchar(20),phno int) /*TO INSERT insert into insert into insert into insert into VALUES INTO emp1 values emp1 values emp1 values emp1 values TABLE*/ ('arun','selam',245436) ('krishna','chennai',287680) ('rahul','erode',213465) ('karthik','madurai',209867)

/*TO CREATE TABLE2*/ create table emp2(ename varchar(20),dest varchar(20),salary int) /*TO INSERT insert into insert into insert into insert into VALUES INTO emp2 values emp2 values emp2 values emp2 values TABLE*/ ('karthik','managing directer',15000) ('saran','manager',10000) ('rahul','asst manager',8500) ('krishna','prj manager',5600)

/*TO VIEW TABLE*/ select * from emp1

/*JOIN OPERATIONS*/ /*INNER JOIN*/ select emp1.ename,emp1.city from emp1 inner join emp2 emp1.ename= emp2.ename on

/*LEFT OUTER JOIN*/ select emp1.ename,emp1.city,emp2.salary from emp1 left outer join emp2 on emp1.ename= emp2.ename

/*RIGHT OUTER JOIN*/ select emp1.city,emp2.dest from emp1 right outer join emp2 emp1.ename= emp2.ename

on

/*FULL JOIN*/ select * from emp1 full join emp2

on emp1.ename= emp2.ename

/*SET OPERATION*/ /* TO CREATE TABLE3 */ create table emp3 (eid int primary key,ename varchar(20),place varchar(20)) /*TO INSERT insert into insert into insert into insert into VALUES INTO TABLE*/ emp3 values(1,'karthik','chennai') emp3 values(2,'ram','chennai') emp3 values(3,'deva','coimbatore') emp3 values(4,'bala','selem')

/* TO CREATE TABLE4 */ create table emp4 (eid int,ename varchar(20),place varchar(20)foreign key(eid) references emp3,phno int) /*TO INSERT insert into insert into insert into insert into VALUES INTO emp4 values emp4 values emp4 values emp4 values TABLE*/ (1,'arun','cbe',3434) (2,'ram','chennai',287680) (3,'rahul','erode',213465) (4,'karthik','chennai',209867)

/*UNION OPERATION*/ (select ename,place from emp3) union (select ename,place from emp4)

/*UNION ALL OPERATION*/ (select ename,place from from emp4)

emp3)

union

all

(select

ename,place

/*INTERSECTION OPERATION */ (select ename from emp3) intersect (select ename from emp4)

(select distinct ename from emp3) intersect (select ename from emp4)

/*EXCEPT OPERATION*/ (select ename from emp3) except (select ename from emp4)

/*TO VIEW TABLE1*/ select * from emp4

4.VIEWS,cursors and functions


/* TO CREATE DATABASE*/ create database empl /* TO CREATE TABLE*/ create table emp (eid int,ename varchar(20),basicpay int) /*TO INSERT insert into insert into insert into insert into VALUES INTO TABLE*/ emp values (1,'arun',10000) emp values (2,'ram',12000) emp values (3,'rahul',15000) emp values (4,'karthik',23000)

/*TO CRATE VIEW*/ create view emply as select eid,ename,basicpay from emp /*TO VIEW NAME WHOSE BASIC PAY GREATER THAN 10000*/ select ename from emply where basicpay>10000

/*TO PERFORM ARTHMETIC OPERATION IN VIEW*/ select ename ,basicpay*2 from emp where eid=3

/*TO UPDATE VALUES INTO YHE VIEW */ update emply set basicpay=30000 where eid=3

/*TO VIEW TABLE*/ select * from emp

/*TO VIEW THE VIEW */ select * from emply

/*TO DROP THE TABLE */ drop table emp /*TO DROP THE VIEW */ drop view emply

You might also like