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

SQL Code

This document contains SQL queries and statements for creating, populating, and querying a database called SuperM. It includes: 1. Queries to select data from tables based on various conditions and filters. 2. Statements for creating tables, inserting sample data, and establishing relationships between tables. 3. More complex queries joining multiple tables. 4. Views and stored procedures created for common queries and calculations. 5. A transaction example inserting new records into a table. The document shows how to structure and manage a basic relational database using SQL. A variety of SQL statements are demonstrated for data definition, manipulation, and querying across multiple tables.

Uploaded by

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

SQL Code

This document contains SQL queries and statements for creating, populating, and querying a database called SuperM. It includes: 1. Queries to select data from tables based on various conditions and filters. 2. Statements for creating tables, inserting sample data, and establishing relationships between tables. 3. More complex queries joining multiple tables. 4. Views and stored procedures created for common queries and calculations. 5. A transaction example inserting new records into a table. The document shows how to structure and manage a basic relational database using SQL. A variety of SQL statements are demonstrated for data definition, manipulation, and querying across multiple tables.

Uploaded by

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

use [SuperM];

SELECT * from Staff WHERE year(EMP_Date)=2003;

SELECT Item_ID,Item_Name,Price from Items WHERE price<10;

SELECT Item_ID,Item_Name,Price from Items WHERE price between 5 and 100;

SELECT Item_ID,Item_Name,Price from Items WHERE price<5 or price>100;

Select Amount_Sold,Item_Name,SName,staff.S_ID
from sales,Items,Staff where Amount_Sold>5 and Sales.S_ID=Staff.S_ID
and Sales.Item_ID=Items.Item_ID;

Select SName,FName from Staff,Sales


where Item_ID='FB9145' and sales.S_ID=staff.S_ID;

Select Item_Name,Model,Amount_Sold from Items,Sales


Where S_ID='FS186'
and sales.Item_ID=Items.Item_ID;

select * from Items where Exp_Date is NUll;


select * from Items where Exp_Date is not NUll;

select * from staff


where SName like '%e%';

select * from staff


where SName like '________%';

use [SuperM];
--qusetin15->retrive Item_ID,Item_Name,Price oder by item name ascending order
select Item_ID,Item_Name,Price from Items ORDER by Item_Name asc ;
--qusetin16->retrive Sales.Item_ID,Item_Name,price,Staff.S_ID,SName oredr by
sname
--ascending order and oreder by item name descending order
select Sales.Item_ID,Item_Name,price,Staff.S_ID,SName from Items,Staff,Sales
where sales.Item_ID=Items.Item_ID AND Sales.S_ID=Staff.S_ID ORDER BY SName
asc,
Item_Name desc;
--qusetin17->retrive item id and item name of sold items
select Sales.Item_ID,Item_Name from items,Sales
where Sales.Item_ID=Items.Item_ID;
--qusetin18-> retrive item id and item name of sold items
--to retrive without repeation
select distinct items.Item_ID,Item_Name from items,Sales
where Sales.Item_ID=Items.Item_ID;

--qusetin19->find the number of employees from staff table


select count(*) as number_of_employees from staff ;

--qusetin20->
select count(*)total_item,min(price)min_price,max(price)max_price,
avg(price)avarage_price,sum(price)total_price from Items,sales
where sales.item_ID=Items.Item_Id ;
--qusetin20->retrive Item_ID,Item_Name,price and Add 15% to price display
--the new NEW_price_with_VAT
select Items.Item_ID,Item_Name,price,
NEW_price_with_VAT=price*0.15+price from Items;
--qusetin22->count price which is greater than 100
select count(*) from items
where price>100 ;
--qusetin23-> count Sname starts with A or M
select count(SName) as name_starts_with_A_or_M from staff
where SName like 'A%'or SName like 'M%';

--qusetin24-> to retrive the name of employee who sold item


select SName from staff
where S_ID in(select distinct S_ID from Sales);
--or--
select distinct staff.S_ID,SName from staff,sales
where sales.S_ID=Staff.S_ID;

--qusetin25-> to retrive the name of employee who have not sold item
select SName from staff
where S_ID not in(select distinct S_ID from Sales);

--create database SuperM;-> to create the database named superM

use [SuperM];->we use this to create the table in SuperM the database

create table Staff(


S_ID char(10) NOT NULL,
SName varchar(20) NOT NULL,
FName varchar(20) NOT NULL,
EMP_Date datetime NOT NULL,
END_Date datetime,
constraint PK_Staff Primary key (S_ID),
);
create table Items(
Item_ID char(8) NOT NULL,
Item_Name varchar(20) NOT NULL,
Model varchar(10) NOT NULL,
Price smallmoney NOT NULL,
EXP_Date datetime,
Stoke_Amount int NOT NULL,
constraint PK_Item Primary key (Item_ID),
);
create table Sales(
S_ID char(10) NOT NULL,
Item_ID char(8) NOT NULL,
Amount_Sold int NOT NULL,
Sale_Date datetime NOT NULL default getdate(),
constraint PK_Sales Primary key (S_ID,Item_ID),
constraint FK_Stuff_Sales Foreign key (S_ID) references Staff(S_ID) on
update cascade,
constraint FK_Items_Sales foreign key (Item_ID) references
Items(Item_ID)on update cascade,
);
use [SuperM];-> to insert data in tha table which was created before in the
data base called SuoerM

--Fill records in the Staff table


insert into Staff(S_ID, SName, FName, EMP_Date)
values ('MC329', 'Zeryihun', 'Asefa', '10/05/2003');
insert into Staff(S_ID, SName, FName, EMP_Date)
values ('FC150', 'Tirunesh', 'Chalachew', '02/25/2003');
insert into Staff(S_ID, SName, FName, EMP_Date)
values ('MS534', 'Asnake', 'Tesfaw', '04/17/2002');
insert into Staff(S_ID, SName, FName, EMP_Date)
values ('FS278', 'Kelemua', 'Jemberu', '01/09/2002');
insert into Staff(S_ID, SName, FName, EMP_Date)
values ('FS186', 'Meseret', 'Beyene', '08/16/2003');

--Fill records in the Items table


insert into Items(Item_ID, Item_Name, Model, Price, Stoke_Amount)
values ('ST9823', 'Dot Pencil', 'DP78', 1.25, 50);
insert into Items(Item_ID, Item_Name, Model, Price, Stoke_Amount)
values ('FB7301', 'Shola Milk', 'SM8', 5.90, 100);
insert into Items(Item_ID, Item_Name, Model, Price, Stoke_Amount)
values ('DT4589', 'B-29 Soap', 'B298', 4.50, 218);
insert into Items(Item_ID, Item_Name, Model, Price, Stoke_Amount)
values ('FB9278', 'Coca Cola', '300ml', 5.50, 167);
insert into Items(Item_ID, Item_Name, Model, Price, Stoke_Amount)
values ('DT7125', 'Omo', 'Om21', 11.50, 84);
insert into Items(Item_ID, Item_Name, Model, Price, Stoke_Amount)
values ('EL4756', 'Electric Stove', 'HP56', 300, 12);
insert into Items(Item_ID, Item_Name, Model, Price, Stoke_Amount)
values ('FB9145', 'Sliced Bread', 'S829', 10.60, 29);

--Fill records in the Sales table


insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('MS534', 'EL4756', 1, '10/21/2003');
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('FS278', 'FB9145', 2, '07/02/2003');
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('FS186', 'DT7125', 5, getdate());
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('MS534', 'ST9823', 10, '05/05/2003');
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('FS278', 'FB7301', 6, '01/12/2003');
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('FS186', 'FB9278', 3, '01/24/2004');
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('MS534', 'FB9145', 8, '10/15/2003');
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('FS278', 'DT4589', 4, '04/04/2004');
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('FS186', 'FB7301', 13, '12/30/2003');
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('FS186', 'FB9145', 3, '12/30/2003');
insert into Sales(S_ID, Item_ID, Amount_Sold, Sale_Date)
values ('FS186', 'DT4589', 9, '02/09/2004');
--create database SuperM;
--drop database SuperM;
use [SuperM];
/*select s_ID,sales.Item_ID,price AS[unit price], Amount_sold,
price*Amount_sold AS[total price]
from sales,Items
where sales.Item_ID = Items.Item_ID;
select s_ID,count(s_ID)AS item_sold
from sales
group by s_ID;
select sales.s_ID,sname,fname,count(*)AS [noof Item sold]
from sales,staff
where sales.s_ID=staff.s_ID
group by sales.s_ID,sname,fname having count(sales.s_ID)>3;
select Item_ID,Item_name,price
from Items
where price <(select avg(price)from Items);
*/
create view employees2([emp_id],[emp-name],[emp_fname])
as
select s_ID,SName,Fname
from staff;
select * from employees2;
create view product12
as
select staff.S_ID,SName,items.Item_ID,Item_Name,price as [unit
price],Amount_sold
,price*Amount_sold AS [total price]
from staff,sales,items;
select * from product12
where staff.S_ID=Staff.S_ID and items.item_id=sales.item_ID;

Stored Procedure
/*1 create a stored procedure that calculates the total priceot items
sold,item name1
,item id,unit price and sold quantities*/
ALTER PROCEDURE CalcTotalPrice
As
--DECLARE @totalprice MONEY
SELECT Items.Item_ID,Price AS
[Unit_Price],Amount_Sold,Total_Price=Price*Amount_Sold
FROM Items,Sales
where Items.Item_ID=Sales.Item_ID;crg code
EXEC CalcTotalPrice;
/*2 create a stored procedure that shows the emplyees ID,and name who slod
the item with ID FB9278 or DT7125 */
Alter PROCEDURE WhoSold
As
SELECT sales.S_ID,SName,Item_ID
FROM Staff,Sales
where staff.S_ID=Sales.S_ID AND (Item_ID='FB9278' or Item_ID='DT7125');
EXEC WhoSold;
/*3 create a procedure empcounter that return the number of employees*/
ALTER PROCEDURE EmpCounter
AS
DECLARE @emps int
SELECT @emps=count(*)
FROM Staff
RETURN @emps;

DECLARE @Employees int


EXEC @Employees=EmpCounter
SELECT NO_of_EMPLOYEES=@Employees;
/*4 create a procedure that displays ther ID and name of emplyees who sold a
given item id*/
CREATE PROCEDURE saler @ItemNo char(8)
AS
SELECT staff.S_ID,SName
FROM Staff,Sales
where Staff.S_ID=Sales.S_ID AND Item_ID=@ItemNo;

DECLARE @ItemNO char(8)


EXEC Saler @ItemNO='DT7125';
/*5 create a procedure that displays 'Expensive' if the item price is greater
than one hundred
,'Normal price' if price is between 50 and 100,'cheap' if price is less than
50 for an item
whose item is given*/
CREATE PROCEDURE PriceCatagory @item char(8)
AS DECLARE @catagory VARCHAR(20)
SELECT price,
if(Price>100)
@catagory ='Expensive'
else if(price>=100 AND price<=100)
@catagory ='Normal price'
else
@catagory ='Cheap'
from Items
where Item_ID=@item
RETURN @catagory;

Transaction
/*6 create a procedure with transaction that adds the following records into
the staff table*/
BEGIN TRANSACTION
insert into Staff(S_ID, SName, FName,EMP_Date)
values ('MS381', 'Abera', 'teshome', '12/12/2003');
insert into Staff(S_ID, SName, FName,EMP_Date)
values ('FS420', 'Tizita', 'Getta', '09/05/2004');

SQL is followed by unique set of rules and guidelines called Syntax. This tutorial gives you a
quick start with SQL by listing all the basic SQL Syntax:
All the SQL statements start with any of the keywords like SELECT, INSERT, UPDATE,
DELETE, ALTER, DROP, CREATE, USE, SHOW and all the statements end with a semicolon
(;).

Important point to be noted is that SQL is case insensitive, which means SELECT and select
have same meaning in SQL statements, but MySQL makes difference in table names. So if you
are working with MySQL, then you need to give table names as they exist in the database.

SQL SELECT Statement:


SELECT column1, column2....columnN
FROM table_name;

SQL DISTINCT Clause:


SELECT DISTINCT column1, column2....columnN
FROM table_name;

SQL WHERE Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION;

SQL AND/OR Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION-1 {AND|OR} CONDITION-2;

SQL IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);

SQL BETWEEN Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE column_name BETWEEN val-1 AND val-2;

SQL LIKE Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE column_name LIKE { PATTERN };

SQL ORDER BY Clause:


SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION
ORDER BY column_name {ASC|DESC};

SQL GROUP BY Clause:


SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name;

SQL COUNT Clause:


SELECT COUNT(column_name)
FROM table_name
WHERE CONDITION;

SQL HAVING Clause:


SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name
HAVING (arithematic function condition);

SQL CREATE TABLE Statement:


CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

SQL DROP TABLE Statement:


DROP TABLE table_name;

SQL CREATE INDEX Statement :


CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...columnN);

SQL DROP INDEX Statement :


ALTER TABLE table_name
DROP INDEX index_name;

SQL DESC Statement :


DESC table_name;

SQL TRUNCATE TABLE Statement:


TRUNCATE TABLE table_name;

SQL ALTER TABLE Statement:


ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};

SQL ALTER TABLE Statement (Rename) :


ALTER TABLE table_name RENAME TO new_table_name;

SQL INSERT INTO Statement:


INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);

SQL UPDATE Statement:


UPDATE table_name
SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE CONDITION ];

SQL DELETE Statement:


DELETE FROM table_name
WHERE {CONDITION};

SQL CREATE DATABASE Statement:


CREATE DATABASE database_name;

SQL DROP DATABASE Statement:


DROP DATABASE database_name;

SQL USE Statement:


USE DATABASE database_name;

SQL COMMIT Statement:


COMMIT;

SQL ROLLBACK Statement:


ROLLBACK;
SQL> SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000 OR age < 25;

You might also like