SQL Code
SQL Code
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;
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;
--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%';
--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);
use [SuperM];->we use this to create the table in SuperM the database
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;
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 IN Clause:
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);