SQL Quiz
SQL Quiz
SQL Quiz
Q-1: Put the keywords Select, From, Order By, Group By, Where, Having in right order of usage in SQL queries. (1 Marks) Ans-1: Example:
select tbl_Roles.ROLEID,tbl_Roles.ROLENAME,tbl_Roles.ROLECODE,tbl_Roles.ISACTIVE from tbl_Roles where ROLEID>0 group by tbl_Roles.ROLEID,tbl_Roles.ROLENAME,tbl_Roles.ROLECODE,tbl_Roles.ISACTIVE having ROLEID>0 order by ROLEID
Q-2: We have one table (Name: Student) with 2 fields in it (StudentName, Marks) where StudentName is primary key. Write a query which will list down the name, marks of the student who has secured the highest marks. (2 Marks) Ans-2:
select top 1 marks, Name from Student order by Marks DESC
Q-3: Read the table structure given below with sample data. PID is the joining field between 2 tables. Write a query to list the total sales value for each category. (2 Marks)
Ans-3:
SELECT FROM Product.ProductCategory, SUM(Sales.SaleValue) AS TotalSaleValue Product INNER JOIN Sales ON Product.PID = Sales.PID GROUP BY Product.ProductCategory
Page 1 of 3
Q-4: We have one table (Name: Student) with 2 fields in it (StudentName, Marks) where StudentName is primary key. Write the SQL queries for following requirements (5X1 Marks) a. Number of students where marks are greater than 75 b. List them in descending order c. List all students with name starting with R d. Insert one record with Values as Name=studentABC & Marks = 87 e. Assuming, the table dont have primary key, identify all names which are duplicate. Ans 4 a) b) c) d) e)
Select COUNT(Name) as StudentCount from Student where Marks >75 Select Name,Marks Select Name,Marks from Student order by Marks Desc from Student where Name like 'R%'
insert into Student(Name,Marks) values ('studentABC',87) SELECT Name, COUNT(Name) AS DuplicateCount FROM Student GROUP BY Name HAVING (COUNT(Name) > 1)
Q-5: For the given table and sample data, provide the outcome of the following queries (3X2 Marks)
Host.HostName, HostMAC.HostMAC Host FULL OUTER JOINHostMAC ON Host.HostID = HostMAC.HostID HostName HostMAC
Host.HostName, HostMAC.HostMAC Host Inner JOINHostMAC ON Host.HostID = HostMAC.HostID HostName HostMAC Page 2 of 3
Output:
Ans-5 : Try Yourelf. Q-6: Given the following tables, Write SQL statement to generate the output to list down the products which has no sale in the month of July 2012. Write queries in 2 different ways. One of the ways should be using left join. (4 Marks)
1. SELECT
PRODUCT1.PDESC FROM SALES1 LEFT JOIN PRODUCT1 ON PRODUCT1.PRODUCTID = SALES1.PRODUCTID where sales1.salesdate < '2012-07-1' or Sales1.salesdate > '2012-07-30' PRODUCT1.PDESC FROM SALES1 INNER JOIN PRODUCT1 ON PRODUCT1.PRODUCTID = SALES1.PRODUCTID where sales1.salesdate < '2012-07-1' or Sales1.salesdate > '2012-07-30'
2. SELECT
Page 3 of 3