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

No Orders: Find Missing Values Tempdb

The document contains a list of SQL queries to retrieve and analyze data from various tables and databases. It includes queries to select customers by city and order count, recent orders, order details by employee and customer, missing values, median values, totals, subtotals, and more. It also provides examples of creating views, functions, CTEs, and other SQL features to perform complex data analysis and reporting.

Uploaded by

sukhpreetn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

No Orders: Find Missing Values Tempdb

The document contains a list of SQL queries to retrieve and analyze data from various tables and databases. It includes queries to select customers by city and order count, recent orders, order details by employee and customer, missing values, median values, totals, subtotals, and more. It also provides examples of creating views, functions, CTEs, and other SQL features to perform complex data analysis and reporting.

Uploaded by

sukhpreetn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

 Select cutomers from Madrid having orders less than 3.

 Select 10 customers who placed orders recently


 Select most recent order of each employee
 Select top 2 most recent order of each employee
 Select top 2 most recent orders of each customers
 Select top 2 most recent orders of customers and customers who made
no orders
 Select customerId, city, category of each customers. The category
value should be ‘no_orders’, ‘upto_two_orders’ and
‘more_than_two_orders’
 Select customers with name having ‘o’ or ‘s’ in their customerid
 Select all customers who didn’t place any orders using except
 Select all customers for whom every employee from ‘usa’ has handled
atleast one order
 Select all orders placed on the last actual order date of the month
 Select all orders with the maximum order date of each employee
 Select most recent order for each northwind employee
 Select all customers from spain that made order
 Select all customers from spain who made no orders
 Select shippers from northwind datebase who did not ship orders to
customer ‘LAZYK’
 Select for each employee, the order with the minimum order id
 Select number of customers for each year
 Select the order year and number of customers with more than 70
customers
 Select order year, number of customers for that year, number of
customers from previous year and growth for each order year
 Select orderyear, number of customers for each year with employee id
as argument and using CTE
 Select the order year and number of customers with more than 70
customers using multiple CTE
 Remove all duplicate rows from customers table using CTE
 Select all possible pairs of employee
 Select for each sale, the sale’s percentage of total quantity sold
and the difference between sale quantity and average quantity of all
sales
 Select order of US customers
 Select customers with no orders
 Select top 2 orders from employee id 3 and 5
 ********* Find missing values
use tempdb;
go

if(OBJECT_ID ('dbo.T1')) is not null


drop table dbo.T1;
go

create table T1
(
keycol int not null primary key,
datacol varchar(10) not null
);
--insert into T1(keycol,datacol)values(1,'e');
insert into T1(keycol,datacol)values(2,'f');
insert into T1(keycol,datacol)values(3,'a');
insert into T1(keycol,datacol)values(4,'b');
insert into T1(keycol,datacol)values(6,'c');
insert into T1(keycol,datacol)values(7,'d');
 Return all employees with managers they report to
 Return customer company name and supplier company name, where the
supplier supplies the products to the consumer
 Returns customer company name and supplier company name , where the
supplier supplied the products to the customer.Also, return the
customers that made no orders
 Select for each year, the sliding total of the previous year’s orders
 Select Country and num cities covered by customers and employee
 Return orders places by customer ‘alfki’ also orders handled by
employee 3
 Create View using CTE
 Create function using CTE
 Select 3 most recent orders
 select most recent 1 percent orders
 select first 3 orders in order of increasing customerid
 select @n most recent orders ,where @n is an argumento to Top
 select average no of monthly orders from orders.
 Select the two most expensive beverages(categoryid =1) for each
supplier
 Select the two most expensive beverages(categoryid =1) for each
supplier using FUNCTION
 Select suppliers who did not supply beverages
 Select suppliers who did not supply beverages using FUNCTION.
 Select lower of the two most expensive beverages(categoryid =1) for
each supplier
 Select lower of the two most expensive beverages(categoryid =1) for
each supplier using FUNCTION
 write a function named AddNumberDays that takes a date and an
integer, then it returns the date added to the number.
 Write a function named GetNumberDays that takes 2 dates and returns
the number of days between them
 Write a function named GetWeekDayName that when given a date, can
find and display the name of the week for the date.
 Write a functinon named ProcessPayroll1 that takes the number of
hours worked in a week.Then the function returns a value that
represents overtime.If the employee worked less than 40 hours, there
is no overtime.If the employee worked for more than 40 hours, the
number over 40 is considered overtime.
 Write a function to find the start and end dates of the week for the
passed date.
 Write a function to get the days in a month
 Write a function to determine whether the year is a leapyear
 Write a function to calculate the days in a year
 Write functin to calculate the time difference in hour
 Write function to calculate the time difference in hour and min
 Write a function to Get Last Day Of Month
 Write function to get the first day of the quarter
 Match each employee’s order with his previous order.
 Match each employee’s order with his previous order using CTE
 Select a random row from Orders table.(Using RANC() and
CHECKSUM(newid()))
 Select the median of the val column for the whole table
 Select the median of the val column for each group
******
if OBJECT_ID('dbo.groups') is not null
drop table dbo.groups;
go

create table dbo.groups


(
groupid varchar(10) not null,
memberid int not null,
string varchar(10) not null,
val int not null,
primary key(groupid,memberid)
);

insert into dbo.groups(groupid,memberid,string,val)


values('a',3,'stra1',6);
insert into dbo.groups(groupid,memberid,string,val)
values('a',9,'stra2',7);
insert into dbo.groups(groupid,memberid,string,val)
values('b',2,'strb1',3);
insert into dbo.groups(groupid,memberid,string,val)
values('b',4,'strb2',7);
insert into dbo.groups(groupid,memberid,string,val)
values('b',5,'strb3',3);
insert into dbo.groups(groupid,memberid,string,val)
values('b',9,'strb4',11);
insert into dbo.groups(groupid,memberid,string,val)
values('c',3,'strc1',8);
insert into dbo.groups(groupid,memberid,string,val)
values('c',7,'strc2',10);
insert into dbo.groups(groupid,memberid,string,val)
values('c',9,'strc3',12);

 Select a row for each customer , with the total quantities in


different column for each year.
use tempdb;
go

if OBJECT_ID('dbo.orders') is not null


drop table dbo.orders;
GO

create table dbo.orders


(
orderid int not null primary key nonclustered,
orderdate datetime not null,
empid int not null,
custid varchar(5) not null,
qty int not null
);
create unique clustered index idx_orderdate_orderid
on dbo.orders(orderdate,orderid);

Insert into orders(orderid,orderdate,empid,custid,qty)


values (30001,'20020802',3,'A',10);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (10001,'20021224',1,'A',12);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (10005,'20021224',1,'B',20);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (40001,'20030109',4,'A',40);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (10006,'20030118',1,'C',14);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (20001,'20030212',2,'B',12);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (40005,'20040212',4,'A',10);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (20002,'20040216',2,'C',20);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (30003,'20040418',3,'B',15);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (30004,'20020418',3,'C',22);
Insert into orders(orderid,orderdate,empid,custid,qty)
values (30007,'20020907',3,'D',30);

 Return total quantities of orders based on dimensions empid,custid


and orderyear using CUBE
 Return total order quantities for the dimensions order year,order
month and order day using ROllUP

Use Adventureworks
 Calculate running total of each sale(This example calculates the
running total amount by summing up all the TotalDue amounts for 
SalesOrderID's less then or equal to the current SalesOrderID).
 Calculate a running subtotals for each OrderDate.( The subtotals are
calculated by summing the TotalDue by OrderId with where the Order
dates are the same)

You might also like