0% found this document useful (0 votes)
47 views

SQL4

The document creates databases and tables to store customer and order data. It inserts sample records, performs joins, updates data, uses built-in functions, and case/order statements to analyze and manipulate the data. It also creates two employee tables and demonstrates union, intersect, and except operators on the tables.

Uploaded by

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

SQL4

The document creates databases and tables to store customer and order data. It inserts sample records, performs joins, updates data, uses built-in functions, and case/order statements to analyze and manipulate the data. It also creates two employee tables and demonstrates union, intersect, and except operators on the tables.

Uploaded by

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

--Create Database

Create Database Customer;

--Use Database
Use Customer;

--Create Table(Create a customer table which comprises of these columns �


�customer_id�, �first_name�,�last_name�, �email�, �address�, �city�,�state�,�zip�)
Create table Customer_Details(
Customer_ID Int Primary key,
Customer_First_Name Varchar(50) not null,
Customer_Last_Name Varchar(50) default'.',
Customer_Email Varchar(70) not null,
Customer_Address Varchar(150) not null,
Customer_City Varchar(50),
Customer_State Varchar(50),
Customer_Zipcode Int
);

--Insert into the table(Insert 5 new records into the table)


Insert into Customer_Details
(Customer_ID,Customer_First_Name,Customer_Email,Customer_Address,Customer_City,Cust
omer_State,Customer_Zipcode)
values(123,'Gabby','[email protected]','No 7
Plaza','San Jose','California',95112);
Insert into Customer_Details values
(456,'Sam','Lisa','[email protected]','20th Ave','Denver','Colorado',80202),
(789,'Zor','Kam','[email protected]','1275 B Block','Atlanta','Georgia',30303),
(901,'Mc','Cool','[email protected]','D 37','Dover','Delaware',19901),
(234,'Holly','Biz','[email protected]','06 7th CT','Boise','Idaho',83702)
;

--Displaying the table with Condition(Select only the �first_name� & �last_name�
columns from the customer table & Select those records where �first_name� starts
with �G� and city is �San Jose)
Select * from Customer_Details;
Select Customer_First_Name,Customer_Last_Name from Customer_Details;
Select * from Customer_Details where Customer_First_Name like 'G%' and
Customer_City like 'San Jose';

--Create Table(Create an �Orders� table which comprises of these columns �


�order_id�, �order_date�, �amount�, �customer_id')
Create Table Orders(
Order_ID int not null,
Order_Date date not null,
Amount int not null,
Customer_ID int foreign key references Customer_Details(Customer_ID)
);

insert into Orders values


(85213,'2023-02-10',25000,123),
(78952,'2023-02-02',32500,456),
(12358,'2023-01-28',12750,789),
(98725,'2023-03-01',69250,901),
(65482,'2023-01-31',42300,234);

-- Make an inner join on �Customer� & �Order� tables on the �customer_id� column
select c.*,o.* from Customer_Details as c
Inner join
Orders as o
on c.Customer_ID=o.Customer_ID;

--Make left and right joins on �Customer� & �Order� tables on the �customer_id�
column
select c.*,o.* from Customer_Details as c
Right join
Orders as o
on c.Customer_ID=o.Customer_ID;

select c.*,o.* from Customer_Details as c


Left join
Orders as o
on c.Customer_ID=o.Customer_ID;

--Update the �Orders� table, set the amount to be 100 where �customer_id� is 3(Dont
have 3 so use 789 instead)
Update Orders set Amount=100 where Customer_ID=789;

-- Use the inbuilt functions and find the minimum, maximum and average amount from
the orders table
select min(Amount) from Orders;

Select max(amount) from Orders;

select avg(amount) from orders;

--create a user-defined function, which will multiply the given number with 10
Create Function Mult_10(@num as int)
returns int
as
begin
return(
@num*10
)
end;

select dbo.mult_10(10);

--use the case statement to check if 100 is less than 200, greater than 200 or
equal to 200 and print the corresponding value
select case
When 100<200 then '100 Is less than 200'
When 100>200 then '100 is more than 200'
When 100=200 then '100 is equal to 200'
else NUll
End;

-- Arrange the �Orders� dataset in decreasing order of amount


Select * from Orders
order by Amount desc;

-- Create a table with name �Employee_details1� and comprising of these columns �


�Emp_id�, �Emp_name�, �Emp_salary�. Create another table with name
�Employee_details2�, which comprises of same columns as first table
Create table Employee_details1(
Emp_id int primary key,
Emp_name varchar(50) not null,
Emp_salary int not null
);

Create table Employee_details2(


Emp_id int primary key,
Emp_name varchar(50) not null,
Emp_salary int not null
);

Insert into Employee_details1 values


(150001,'Kumar',75000),
(150002,'Suresh',50000),
(150003,'Hari',150000),
(150004,'Bilal',80000);

insert into Employee_details2 values


(150001,'Ahmed',95750),
(150002,'Rajesh',35000),
(150007,'Satish',15000);

-- Apply the union operator on these two tables


Select * from Employee_details1
Union
Select * from Employee_details2;

--Apply the intersect operator on these two tables


Select Emp_id from Employee_details1 where Emp_salary=50000
intersect
Select Emp_id from Employee_details2 where Emp_salary=35000;

-- Apply the except operator on these two tables


Select * from Employee_details1
Except
Select * from Employee_details2;

You might also like