0% found this document useful (0 votes)
126 views3 pages

SQL Scenarios

This document contains SQL queries and assignments related to analyzing sales data from an adventureworks database. It includes queries to display employee data, get counts by department, show manager relationships, track department changes, and analyze individual and store customer data by country, state, sales amounts and years. It also includes assignments to create tables, stored procedures, triggers and functions to analyze student and employee data.

Uploaded by

roshanrise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views3 pages

SQL Scenarios

This document contains SQL queries and assignments related to analyzing sales data from an adventureworks database. It includes queries to display employee data, get counts by department, show manager relationships, track department changes, and analyze individual and store customer data by country, state, sales amounts and years. It also includes assignments to create tables, stored procedures, triggers and functions to analyze student and employee data.

Uploaded by

roshanrise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1)--display employeeid,employeename,departname from adventureworks database

2)--get employee count by department name for active employees


3)--get employee name and respective manager's name
4)--get the empid,empname,current dept of employees who changed their department
5)--get the
empname,managername,empaddress,state,country,manageraddress,managerstate,managercou
ntry
6)--display country,state,empcount
7)--select prodid,name,subcatogaryname,catagoryname
8)--select categaryname,product count
9)--get custid,name,phoneno,email
10)--STOREid,name,contactname,phone,email
11)--get storeid,name,contactname,phone,email for store having more than one
storeid
12)--display the individual and store customers by country
13)--display stores having multiple contacts(email,phone)
14)--display categoryname,salesamount
15)--display categoryname,subcategory,salesamount
16)--display year,category,sales
17)--display top products in 2003 and display productid,name,sales of 2003 and
corresponding sales for 2004
18)--for the year 2003 display salespersonname,accessories sales ,bike
sales,componentsales, clothingsales

19)RESELLER SALES
--display country,state,store,sales2003 and sales2004
20)-- ABOVE QUERY USING CASE STATEMENT
21)--For the year 2003 display the INDIVIDUAL custname,salesamount ,phone,email,
aboveavg(Y/N)
--PER ORDER
22)--sum(case when year(ssoh.orderdate)=2003 then ssoh.totaldue else 0 end)as
saleamount2003,
23)--For the year 2003 display the INDIVIDUAL custname,salesamount ,phone,email,
aboveavg(Y/N)
--PER CUSTOMER
24)--sum(case when year(ssoh.orderdate)=2003 then ssoh.totaldue else 0 end)as
saleamount2003,
25)
--display salespersonname,ytdsale,mtdsale,pyytdsale,pymtdsale
26)--2003,individual customers
--display country,newcustomerscount,oldcustomerscount,totalcustomerscount
27)--sum(case when a.customerid is null then 1 else 0 end)+sum(case when
a.customerid is not null then 1 else 0 end ) as total
28)--search INDIVIDUAL CUSTOMERS when the below parameters are entered...(searching
for customers starting with...)
--display firstname,lastname,city,state,country
29)--INDIVIDUAL SALES
--display productid,productname,rank2003,rank2004 (use the below rank function)
30)--for the above scenario partition within the category name display the top 2
ranks for 2003
31)--display top 5 rows for 'CUSTOMERTYPE='S' (top 5 store customers) , 'I'
(top 5 individual customers) (TOTAL 10 ROWS)
--CUSTID,CUSTNAME,SALEAMOUNT,RANK
32)
--ALL COUNTRIES
--ONLY INDIVIDUAL CUSTOMER
--display count for custadded2003,custlost2003,custadded2004,custlost2004
33)--below2003 customers
34)--in2003 customers
35)--below2004 customers
36)--in2004 customers
37)ASSIGNMENT

--create table1 studentid,name table2 subid,subname table3


studentid,subid,marks
--AND DISPLAY THE STUDENT ID AND RANK HE GOT IN TOTAL...
--IF TWO STUDENTS GOT SAME RANK THEN GIVE THE RANK BASED ON THEIR MARKS IN
MATHS,PHYSICS AND CHEMISTRY

--CREATING STUDENTS TABLE


/*create table students(
studentid int not null primary key,
studentname varchar(15)) */

--CREATING SUBJECTS TABLE


/*create table subjects(
subjectid int not null primary key,
subjectname varchar(10)) */

--CREATING STUDENTMARKS TABLE


/*create table studentmarks(
studentid int,subjectid int,marks int) */

--to get the correct logic we have to get a table which is derived from the below
query
(--get studentid,maths,physics,chemistry,total)
-----------------------------------------------
--MAIN QUERY

--create temporary table


-------------------------------------------------
create table #students
(
rank int identity(1,1),
studentid int,
maths int,
physics int,
chemistry int,
total int
)
---------------------------------------------------
--inserting the values into the temporary table
insert into #students
--get studentid,maths,physics,chemistry,total
---------------------------------------------------

create table studentrank


(rank int, studentid int) --creating a permanent table to insert the values from
#students which is temporary table
insert into studentrank --inserting values from the temporary table
select rank,studentid from #students --inserting values from the temporary
table

38)--WITH THE ABOVE SAME SCENARIO CREATE A STORED PROCEDURE


39)--create function which returns empid,empname,managername provided departmentid
40)--now create a trigger on the 'empt' table on which if any action INSERT,
DELETE, UPDATE event happens then the data should be recorded into
'triggerhistory' table
41)--create a trigger on 'emp' table to record all the updations in 'empdeptaudit'
table
--empdeptaudit table: empid,empname,deptname,action,updatedby,date
--emp table: empid,empname, deptid
--dept table: deptid,deptname

42)--assignment
--write a function that returns table when provided names
--ex: getvaluetable('raja,king')
--output: nocolumnname
-- raja
-- king

43)--insert values into emp ,dept tables using INSTEAD OF trigger


--check if the department is present or not...then only insert the values
--also check if the salary is between 10k and 15k

44)--assignment
--individual customers
--create function getcustomers(pageno,recordsperpage)
--which fetches custid,name,phone,email

You might also like