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

--1.Create database

dbms

Uploaded by

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

--1.Create database

dbms

Uploaded by

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

--1.

Create database
CREATE DATABASE SalesOrder

--2.Use the database


CREATE TABLE dbo.Customer(
--3.Create tables
CustomerID int NOT NULL primary key,
CustomerFirstName varchar(50) NOT null,
CustomerLastName varchar(50) NOT null,
CustomerPhoneNumber SMALLINT null,
CustomerState varchar(50) NOT null,
CustomerCountry varchar(50) NOT null
);

CREATE TABLE dbo.Stock(


StockID tinyint NOT null primary key,
StockName varchar(50) NOT null,
StockDescription varchar(255) null,
);

CREATE TABLE dbo.Employee(


EmployeeID tinyint NOT null primary key,
EmployeeFirstName nvarchar(50) NOT NULL,
EmployeeLastName nvarchar(50) NOT NULL,
EmployeeAddress nvarchar(100) null,
);

CREATE TABLE dbo.EmployeeDemographics(


EmployeeID tinyint NOT null primary key,
EmployeeDemographicsID tinyint NOT null,
EmployeeSalary nvarchar(50) NOT NULL,
EmployeeGender char(1) NOT NULL,
EmployeeJobTitle varchar(50) null,
EmployeeAge char(2) null,
);

CREATE TABLE dbo.sale(


SaleID tinyint not null primary key,
CustomerId int not null references Customer(CustomerID),
StockID tinyint NOT null references Stock(StockID),
EmployeeID tinyint not null references Employee(EmployeeID),
SaleDate date not null,
SaleQuantity int not null,
SaleUnitPrice smallmoney not null);

ALTER TABLE Customer ADD PRIMARY KEY(ID);

--Insert into Table


INSERT into Customer(CustomerFirstName, CustomerLastName, CustomerPhoneNumber,
CustomerState, CustomerCountry)
Values('Ruka', 'Kawai', '+81320348245', 'Honshu','Jpana');
INSERT into Customer(CustomerFirstName, CustomerLastName, CustomerPhoneNumber,
CustomerState, CustomerCountry)
Values('Pharita', 'Chaikong', '+6684406567', 'Bangkok','Thailand');
INSERT into Customer(CustomerFirstName, CustomerLastName, CustomerPhoneNumber,
CustomerState, CustomerCountry)
Values('Asa', 'Enami', '+81301388247', ' Tokyo','Japan');
INSERT into Customer(CustomerFirstName, CustomerLastName, CustomerPhoneNumber,
CustomerState, CustomerCountry)
Values('Ahyeon', 'Jung', '+8210986201028', 'Gangwon-do','South Korea');
INSERT into Customer(CustomerFirstName, CustomerLastName, CustomerPhoneNumber,
CustomerState, CustomerCountry)
Values('Chloe', 'Lee', '+12125551234', 'New York City','USA');
INSERT into Customer(CustomerFirstName, CustomerLastName, CustomerPhoneNumber,
CustomerState, CustomerCountry)
Values('Ella', 'McKenzie Gross', '+13105551234', 'California','USA');
INSERT into Customer(CustomerFirstName, CustomerLastName, CustomerPhoneNumber,
CustomerState, CustomerCountry)
Values('Danielle', 'Marsh', '+61220891546', 'New South Wales','Australia');
INSERT into Customer(CustomerFirstName, CustomerLastName, CustomerPhoneNumber,
CustomerState, CustomerCountry)
Values('Disha', 'Pathani', '+919810309395', 'Mumbai','India');
INSERT into Customer(CustomerFirstName, CustomerLastName, CustomerPhoneNumber,
CustomerState, CustomerCountry)
Values('Priyanka', 'Karki', '+9779841767076', 'Lalitpur','Nepal');

--1.Create database
CREATE DATABASE SalesOrder

--2.Use the database


CREATE TABLE dbo.Customer(
--3.Create tables
CustomerID int NOT NULL primary key,
CustomerFirstName varchar(50) NOT null,
CustomerLastName varchar(50) NOT null,
CustomerPhoneNumber varchar(50) null,
CustomerState varchar(50) NOT null,
CustomerCountry varchar(50) NOT null
);

CREATE TABLE dbo.Stock(


StockID tinyint NOT null primary key,
StockName varchar(50) NOT null,
StockQuantity nvarchar(255) NOT null,
StockStatus varchar(50) null,
);

CREATE TABLE dbo.Employee(


EmployeeID tinyint NOT null primary key,
EmployeeFirstName nvarchar(50) NOT NULL,
EmployeeLastName nvarchar(50) NOT NULL,
EmployeeAddress nvarchar(100) null,
);
ALTER TABLE EmployeeDemographics
--alter column EmployeeGender nvarchar(10)
alter column EmployeeAge int;
alter column EmployeeSalary varchar(100);
CREATE TABLE dbo.EmployeeDemographics(
EmployeeID tinyint NOT null primary key,
EmployeeDemographicsID tinyint NOT null,
EmployeeSalary nvarchar(50) NOT NULL,
EmployeeGender char(3) NOT NULL,
EmployeeJobTitle varchar(50) null,
EmployeeAge char(2) null,
);

CREATE TABLE dbo.sale(


SaleID tinyint not null primary key,
CustomerId int not null references Customer(CustomerID),
StockID tinyint NOT null references Stock(StockID),
EmployeeID tinyint not null references Employee(EmployeeID),
SaleDate date not null,
SaleQuantity int not null,
SaleUnitPrice smallmoney not null);

--ALTER TABLE Customer


--Insert into Table
INSERT INTO Customer
Values(1, 'Ruka', 'Kawai', '+81320348245', 'Honshu','Jpana');
INSERT INTO Customer
Values(2, 'Pharita', 'Chaikong', '+6684406567', 'Bangkok','Thailand');
INSERT INTO Customer
Values(3, 'Asa', 'Enami', '+81301388247', ' Tokyo','Japan');
INSERT INTO Customer
Values(4, 'Ahyeon', 'Jung', '+8210986201028', 'Gangwon-do','South Korea');
INSERT INTO Customer
Values(5, 'Chloe', 'Lee', '+12125551234', 'New York City','USA');
INSERT INTO Customer
Values(6, 'Ella', 'McKenzie Gross', '+13105551234', 'California','USA');
INSERT INTO Customer
Values(7, 'Danielle', 'Marsh', '+61220891546', 'New South Wales','Australia');
INSERT INTO Customer
Values(8, 'Disha', 'Pathani', '+919810309395', 'Mumbai','India');
INSERT INTO Customer
Values(9, 'Priyanka', 'Karki', '+9779841767076', 'Lalitpur','Nepal');

--Insert into Table


INSERT INTO Stock
Values(101, 'Product AB', '120', 'Stock Sufficient');
INSERT INTO Stock
Values(102, 'Product BC', '500', 'Re-Stock Sufficient');
INSERT INTO Stock
Values(103, 'Product CD', '69', 'Stock Sufficient');
INSERT INTO Stock
Values(104, 'Product DE', '85', 'Stock Sufficient');
INSERT INTO Stock
Values(105, 'Product EF', '109', 'Stock Sufficient');
INSERT INTO Stock
Values(106, 'Product FG', '990', 'Re-Stock Sufficient');
INSERT INTO Stock
Values(107, 'Product GH', '670', 'Stock Sufficient');
INSERT INTO Stock
Values(108, 'Product HI', '540', 'Stock Sufficient');
INSERT INTO Stock
Values(109, 'Product IJ', '880', 'Re-Stock Sufficient');

--Insert into Table


INSERT INTO Employee
Values(101, 'Rudra', 'Thakur', 'Tinkune');
INSERT INTO Employee
Values(102, 'Pravita', 'Dahal', 'Lokanthali');
INSERT INTO Employee
Values(103, 'Rami', 'Subedi', 'Kalanki');
INSERT INTO Employee
Values(104, 'Grace', 'Lee', 'Bhaktapur');
INSERT INTO Employee
Values(105, 'Slesha', 'Basnet', 'Jhamsikhel');
INSERT INTO Employee
Values(106, 'Aditya', 'Neraula', 'Gausala');
INSERT INTO Employee
Values(107, 'Rohit', 'Dhimal', 'Basundhara');
INSERT INTO Employee
Values(108, 'Alexender', 'Lightwood', 'Jhamsikhel');
INSERT INTO Employee
Values(109, 'Mathew', 'Magar', 'Chabahil');

--Insert into Table


INSERT INTO EmployeeDemographics
Values(1,101, '30000', 'Male', 'HR', 27);
INSERT INTO EmployeeDemographics
Values(2,102, '20000', 'Female', 'Accountant', 25);
INSERT INTO EmployeeDemographics
Values(103, '25000', 'Female', 'IT', 23);
INSERT INTO EmployeeDemographics
Values(104, '55000', Female, 'Manager', 35);
INSERT INTO EmployeeDemographics
Values(105, '15000', 'Others', 'Cleaner', );
INSERT INTO EmployeeDemographics
Values(106, '45000', 'Male', 'PR');
INSERT INTO EmployeeDemographics
Values(107, '30000', 'Male', 'IT');
INSERT INTO EmployeeDemographics
Values(108, '40000', 'Others', 'Team Leader');
INSERT INTO EmployeeDemographics
Values(109, '55000', 'Male', 'IT');

--Insert into Table


INSERT INTO
sale(SaleID,CustomerID,StockID,EmployeeID,SaleDate,SaleQuantity,SaleUnitPrice)
Values(1,1,101,101,'2024-11-11',10,'10');
INSERT INTO
sale(SaleID,CustomerID,StockID,EmployeeID,SaleDate,SaleQuantity,SaleUnitPrice)
Values(2,2,102,102,'2024-01-12',10,'20');
INSERT INTO
sale(SaleID,CustomerID,StockID,EmployeeID,SaleDate,SaleQuantity,SaleUnitPrice)
Values(3,3,103,103,'2024-02-18',15,'10');
INSERT INTO
sale(SaleID,CustomerID,StockID,EmployeeID,SaleDate,SaleQuantity,SaleUnitPrice)
Values(4,4,104,104,'2024-07-13',20,'40');
INSERT INTO
sale(SaleID,CustomerID,StockID,EmployeeID,SaleDate,SaleQuantity,SaleUnitPrice)
Values(5,5,105,105,'2024-01-20',10,'10');
INSERT INTO
sale(SaleID,CustomerID,StockID,EmployeeID,SaleDate,SaleQuantity,SaleUnitPrice)
Values(6,6,106,106,'2024-06-19',30,'10');
INSERT INTO
sale(SaleID,CustomerID,StockID,EmployeeID,SaleDate,SaleQuantity,SaleUnitPrice)
Values(7,7,107,107,'2024-04-12',40,'40');
INSERT INTO
sale(SaleID,CustomerID,StockID,EmployeeID,SaleDate,SaleQuantity,SaleUnitPrice)
Values(8,8,108,108,'2024-10-12',230,'20');

--5.View specific row


--top: show only the first two
SELECT top 2 * FROM Customer
--top 40 percent:also means show the first two
SELECT top 40 percent * From Customer

--6.View specific column


--sort result(by default is ascending)
SELECT CustomerFirstName, CustomerLastName from Customer
order by CustomerLastName desc
SELECT CustomerFirstName, CustomerLastName from Customer
order by 1,2 desc --Order by based on column no. without typing column name
--distinct:only show unique values
SELECT distinct CustomerLastName from Customer order by CustomerLastName

--7.Save table to another table


--into file_name:save result in another table (BASE TABLE)
SELECT distinct CustomerLastName into temp from Customer order by CustomerLastName
SELECT * FROM temp --see the table (data type will remain)

--8.Like( Searching Something)


--(Underscore sign)_ is only specific for one character only
--(percent sign)% represent zero, one, or multiple characters
SELECT * FROM Customer
where CustomerLastName like '_E%'

--9.In(search something)
--SELECT Multiple items
SELECT * FROM Customer
where CustomerLastName in ('Kawai', 'Enami', 'Jung')

--10.>(Search Something)
SELECT * FROM Customer
where CustomerLastName> 'Kawai' or CustomerLastName> 'Jung'

--15.Count
--return the number of rows in a table
--As means aliasing, temporary giving name to a customer/table
SELECT COUNT (*) as [Number of Records] from Customer
where CustomerFirstName like 'R%'

--16.Sum
SELECT sale.EmployeeID, EmployeeFirstName, EmployeeLastName, COUNT(*)
FROM sale, Employee
where sale.employeeid=employee.employeeid
group by sale.EmployeeID,EmployeeFirstName,EmployeeLastName

SELECT * FROM Customer

--17count month
select month(saledate) as[Month],count(*)as[Number of sale],
sum(salequantity * saleunitprice)as[Total Amount]
from sale
group by month(saledate)

--18.max
SELECT MAX (EmployeeSalary)
FROM EmployeeDemographics

--19.min
SELECT MIN(EmployeeSalary)
FROM EmployeeDemographics
--20.average
SELECT AVG(EmployeeSalary)
FROM EmployeeDemographics

--21.having
SELECT EmployeeJobTitle, COUNT(EmployeeJobTitle)
FROM EmployeeDemographics ED
JOIN EmployeeSalary ES
ON ED.EmployeeID = ES.EmployeeID
GROUP BY EmployeeJobTitle HAVING
COUNT(EmployeeJobTitle) > 1

SELECT JobTitle, AVG(Salary)


FROM EmployeeDemographics ED
JOIN EmployeeSalary ES
ON ED.EmployeeID = ES.EmployeeID
GROUP BY JobTitle
HAVING AVG(Salary) > 25000
ORDER BY AVG(Salary)

--22.Change data type temporary for use


--CAST(expression AS datatype(length))
SELECT CAST('2017-08-25 00:00:00.000' AS date)
--CONVERT(data_type(length), expression, style)
SELECT Convert(date, '2017-08-25 00:00:00.000')
SELECT EmployeeFirstName, EmployeeLastName, EmployeeDemographicsAge;

You might also like