You are on page 1of 3

===========Microsoft SQL================

Open a file?
SELECT 'hello world'

User Creation?
select user (To see user)
select * from sys.server_principals
create user Umesh identified by dbo

Create Database?
create database sample

Modify Name of datebase?


alter database sample1 modify name = sample3
System Defined Store Procedure for name change:
execute sp_renamedb 'sample3','sample4'

Creation of Table?
create table Excel (id int, name varchar(50), dept varchar (50), mob int , address varchar (50))

Change Table Name?


execute sp_rename 'Old Name','New Name'
EXEC sp_rename 'Excel_2.name', 'Name', 'COLUMN';
EXEC sp_rename 'Excel_2.id', 'Id', 'COLUMN';

To insert statement to? (enter values in table)


insert into Excel (id , name,dept,mob,address) values (1 , 'Balu', 'RD' , 1892626341, 181)
insert into Excel (id , name,dept,mob,address) values (2 , 'Bala', 'RD' , 1892626342, 182)
insert into Excel (id , name,dept,mob,address) values (3 , 'Ramu', 'RD' , 1892626343, 183)
insert into Excel (id , name,dept,mob,address) values (4 , 'Rama', 'RD' , 1892626344, 184)
insert into Excel (id , name,dept,mob,address) values (5 , 'Sita', 'RD' , 1892626345, 185)
insert into Excel (id , name,dept,mob,address) values (6 , 'Hari', 'RD' , 1892626346, 186)
insert into Excel (id , name,dept,mob,address) values (7 , 'juli', 'RD' , 1892626347, 187)
insert into Excel (id , name,dept,mob,address) values (8 , 'Babu', 'RD' , 1892626348, 188)
insert into Excel (id , name,dept,mob,address) values (9 , 'bosu', 'RD' , 1892626349, 189)
insert into Excel (id , name,dept,mob,address) values (10 , 'Lilly', 'RD' , 1892626350, 190)
insert into sample Country value (ind)

To select Values in table?


select * from Excel
select Country from Excel
select * from Excel where id = 4
select * from Excel where id = 4 and name = 'Rama'
select * from Excel where id = 4 or name = 'Rama'
select * from Excel where id between 2 and 5
select * from Excel where id not between 2 and 5

Update table?
update Excel set name = 'Bimu' where id = 1
update Excel set name = 'lilly' where id = 1 or id 2
update Excel set name = 'lilly' where id = 1 and id 2
Delete statement/data?
delete from sample where id = 7
Add coloum in table?
alter table sample add phone int
alter table sample drop column salary
alter table sample alter column name nchar (5)
Delete table?
truncate table sample (it will delete entire data in table)
drop table sample (it will delete entire table)
delete from sample where id is null (it will delete pritucular value in table)
Primary Key?
It will help to avoid duplicate records.
create Table Excel_2 (id int not null primary key, name varchar(50))
foreign Key?
create Table Excel_2 (id int not null primary key, name varchar(50), Dept int , foreign key references Room int )
foreign key references
create Table Excel_3 (City int not null primary key, name varchar(50), dept varchar (50), mob int ,Address varchar (50) , Id int foreign key
references Excel_2 (Id) )
Unique /Check ?
create table Excel_4 (Std int Unique , Name varchar(50), check (Std >0) )
where operator?
select* from Excel_1 where Address =181
and/or operator?
select* from Excel_1 where Id =1 and Address =185
select* from Excel_1 where Id =1 or Address =185
between/not between operator?
select* from Excel_1 where Id between 1 and 5
select* from Excel_1 where Address not between 184 and 186
select* from Excel_1 where Address not between 184 and 186 and not Name in ('Balu')
Coalesce function?
This function return non null values.
select Id, Coalesce (FirstName,MiddleName,LastName) as Fullname from Excel_5
Union/Union All? It will help to aviod duplicats in table
In Union we can avoid duplicats
select * from Excel_India
Union
select * from Excel_US
In Union All we can see all inclute with duplicats
select * from Excel_India
Union
select * from Excel_US

TOP/WILDCARD

select top 3* from Excel_1


select top 3 Name from Excel_1
select top 50 percent Name from Excel_1

WILDCARDS is %,_, [Charlist]


select Name from Excel_1 where Name like 'R%'
select Name from Excel_1 where Name like '%u'
select Name from Excel_1 where Name like '_A_U'
select Name from Excel_1 where Name like '[A<B<L]%'
select Name from Excel_1 where Name like '[A-L]%'
select Name from Excel_1 where Name not like 'R%'
--------------------------------------------------------------------------
JOINTS: inner ,left,right, full outer joins

select * From Table_6


create table Table_6 (Id int, Name varchar(50), DeptId int)
insert into Table_6 (Id,Name, DeptId ) values (1,'Hari',1),(2,'juli',2),(3,'Babu',3),(4,'Bosu',4),(5,'juli',5)

select * From Table_7

create table Table_7 (DeptId int, Dept varchar(50))


insert into Table_7 (DeptID,Dept) values (1,'RF'), (2,'RA' ), ( 3,'RB'), (4, 'RC') , (5,'RD')

INNER JOIN

SYNTAX 1.

select * from Table_6


inner join
Table_7 on Table_6.DeptId = Table_7.DeptId

SYNTAX 2.

select Id, Name,Table_6.DeptId,Table_7.Dept from Table_6


inner join
Table_7 on Table_6.DeptId = Table_7.DeptId

TO GET SAME DEPTID IN A TABLE

select Id, Name,Table_6.DeptId,Table_7.Dept from Table_6


inner join
Table_7 on Table_6.DeptId = Table_7.DeptId and Table_7.DeptId =4

TO GET DEPTID IN ORDER


select Id, Name,Table_6.DeptId,Table_7.Dept from Table_6
inner join
Table_7 on Table_6.DeptId = Table_7.DeptId order by Table_7.DeptId

TO GET DEPTID IN assindeing Order/Decencing Order

select Id, Name,Table_6.DeptId,Table_7.Dept from Table_6


inner join
Table_7 on Table_6.DeptId = Table_7.DeptId order by Table_7.DeptId asc

select Id, Name,Table_6.DeptId,Table_7.Dept from Table_6


inner join
Table_7 on Table_6.DeptId = Table_7.DeptId order by Table_7.DeptId desc

select * From Table_6


full outer join
Table_7 on Table_6.DeptId = Table_7.DeptId

select * From Table_6


left outer join
Table_7 on Table_6.DeptId = Table_7.DeptId

select * From Table_6


right outer join
Table_7 on Table_6.DeptId = Table_7.DeptId

DATA FORMATE : Returns a datetime value containing the date and time of the computer on which the instance
of SQL Server runs

SELECT SYSDATETIME() AS SYS_DATE :


,CURRENT_TIMESTAMP AS SYS_CURRENT
,SYSDATETIMEOFFSET() AS SYS_OFFSET
,SYSUTCDATETIME() AS SYS_UTC
,GETDATE() AS SYS_GETUTC
,GETUTCDATE() AS SYS_GETUTC;

COPY COMUMN INTO ANOTHR TABLE

USE AdventureWorks2019;
GO
CREATE TABLE dbo.EmployeeSales
( BusinessEntityID varchar(11) NOT NULL,
SalesYTD money NOT NULL
);
GO
INSERT INTO dbo.EmployeeSales
SELECT BusinessEntityID, SalesYTD
FROM Sales.SalesPerson;
GO
----------------------------------------------------------------------------------------------------------------------------------------------------------------
COPY TABLE FORM ONE DB TO ANOTHER DB

Select * into sample.dbo.Table_Name from AdventureWorks2019.dbo.A

You might also like