0% found this document useful (0 votes)
74 views6 pages

SQL Practice Questions for Students

Uploaded by

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

SQL Practice Questions for Students

Uploaded by

vyash2214
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • SQL Practice Questions - School and Admin Tables
  • SQL Commands - Company and Customer Tables
  • Administrative SQL Questions
  • Shop and Accessories SQL Queries
  • Items and Traders SQL Exercises
  • Carden and Customer Data Queries

Swamy’s School

Madanandapuram, Chennai 600125


Questions for practice

1. Consider the following tables SCHOOL and ADMIN and answer this question:
Give the output the following SQL queries:

1. Select Designation Count (*) From Admin Group By Designation Having Count (*) <2;
2. SELECT max (EXPERIENCE) FROM SCHOOL;
3. SELECT TEACHER FROM SCHOOL WHERE EXPERIENCE >12 ORDER BY TEACHER;
4. SELECT COUNT (*), GENDER FROM ADMIN GROUP BY GENDER;
2. Write SQL queries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which are based
on the tables TRANSPORT and TRIP

• PERKS is Freight Charages per kilometer • NOP is number of travellers travelled in


• TTYPE is Transport Vehicle Type vehicle
• NO is Driver Number • TDATE is Trip Date
• KM is Kilometer travelled.
a) To display NO, NAME, TDATE from the table TRIP in descending order of NO.
© Swamy’s Group of Schools, Porur, Chennai 1|Page
b) To display the NAME of the drivers from the table TRIP who are traveling by transport vehicle
with code 101 or 103.
c) To display the NO and NAME of those drivers from the table TRIP who travelled between ‘2015-
02-10’ and ‘2015-04-01’.
d) To display all the details from table TRIP in which the distance travelled is more than 100 KM in
ascending order of NOP
e) SELECT COUNT (*), TCODE From TRIP GROUP BY TCODE HAVNING Count (*) > 1;
f) SELECT DISTINCT TCODE from TRIP;
g) SELECT [Link], NAME, TTYPE FROM TRIP A, TRANSPORT B WHERE A. TCODE = B. TCODE AND
KM < 90;
h) SELECT NAME, KM *PERKM FROM TRIP A, TRANSPORT B
WHERE A. TCODE = B. TCODE AND A. TCODE = 105′;
3. Write SQL commands for the queries (i) to (iv) and output for (v) & (viii) based on a table
COMPANY and CUSTOMER.

1. To display those company name which are having prize less than 30000.
2. To display the name of the companies in reverse alphabetical order.
3. To increase the prize by 1000 for those customers whose name starts with “S”?
4. To add one more column totalprice with decimal] 10,2) to the table customer
5. SELECT COUNT(*) , CITY FROM COMPANY GROUP BY CITY;
6. SELECT MIN(PRICE), MAX(PRICE) FROM CUSTOMER WHERE QTY>10;
7. SELECT AVG(QTY) FROM CUSTOMER WHERE NAME LIKE “%r%;
8. SELECT PRODUCTNAME,CITY, PRICE FROM COMPANY, CUSTOMER WHERE
COMPANY. CID=[Link] AND PRODUCTNAME=”MOBILE”;
4. Consider the following tables SCHOOL and ADMIN and answer this question:

© Swamy’s Group of Schools, Porur, Chennai 2|Page


Write SQL statements for the following:
1. To display TEACHERNAME, PERIODS of all teachers whose periods are more than 25.
2. To display all the information from the table SCHOOL in descending order of experience.
3. To display DESIGNATION without duplicate entries from the table ADMIN.
4. To display TEACHERNAME, CODE and corresponding DESIGNATION from tables SCHOOL and
ADMIN of Male teachers.
5. Write SQL commands for the queries (i) to (iv) and output for (v) to (viii) based on the tables
Watches’ and Sale given below.

1. TO DISPLAY ALL THE DETAILS OF THOSE WATCHES WHOSE NAME ENDS WITH ‘TIME’
2. TO DISPLAY WATCH’S NAME AND PRICE OF THOSE WATCHES WHICH HAVE PRICE RANGE IN
BE-TWEEN 5000-15000.
3. TO DISPLAY TOTAL QUANTITY IN STORE OF UNISEX TYPE WATCHES.
4. TO DISPLAY WATCH NAME AND THEIR QUANTITY SOLD IN FIRST QUARTER;
5. SELECT MAX (PRICE), MIN(QTY_STORE) FROM WATCHES;
6. SELECT QUARTER, SUM(QTY SOLD) FROM SALE GROUP BY QUARTER;
7. SELECT WATCH_NAME, PRICE, TYPE FROM WATCHES W, SALE S WHERE W.
WAT£H1D!=[Link]; (viii) SELECT WATCH_NAME, QTYSTORE, SUM (QTY_SOLD),
QTY_STORESUM (QTYSOLD) “STOCK” FROM WATCHES W, SALE S WHERE W. WATCHID =
[Link] GROUP BY [Link];

© Swamy’s Group of Schools, Porur, Chennai 3|Page


6. Answer the questions (a) and (b) based on the following tables SHOP and ACCESSORIES.

(a) Write the SQL queries:


1. To display Name and Price of all the Accessories in ascending order of their Price.
2. To display Id and SName of all Shop located in Nehru Place.
3. To display Minimum and Maximum Price of each Name of Accessories.
4. To display Name, Price of all Accessories and their respective SName where they are
available.
(b) Write the output of the following SQL
1. SELECT DISTINCT NAME FROM ACCESSORIES WHERE PRICE> =5000;
2. SELECT AREA, COUNT(*) FROM SHOPPE GROUP BY AREA;
3. SELECT COUNT (DISTINCT AREA) FROM SHOPPE;
4. SELECT NAME, PRICE*0.05 DISCOUNT FROM ACCESSORIES WHERE SNO IN (‘S02‘,S03‘);
7. Write SQL queries for:

1. To display name, fee, gender, joinyear about the applicants, who have joined before 2010.
2. To display names of applicants, who are playing fee more than 30000.
3. To display names of all applicants in ascending order of their joinyear.
4. To display the year and the total number of applicants joined in each YEAR from the table
APPLICANTS.
5. To display the C_ID (i.e., CourselD) and the number of applicants registered in the course
from the APPLICANTS and table.
6. To display the applicant’s name with their respective course’s name from the tables
APPLICANTS and COURSES.
7. Give the output of following SQL statements:
o SELECT Name, Joinyear FROM APPLICANTS WHERE GENDER=’F’ and C_ID=’A02′;
o SELECT MIN (Joinyear) FROM APPLICANTS WHERE Gender=’m’;
o SELECT AVG (Fee) FROM APPLICANTS WHERE C_ID=’A0T OR C_ID=’A05′;
o SELECT SUM- (Fee), C_ID FROM C_ ID GROUP BY C_ID HAVING COUNT(*)=2;

© Swamy’s Group of Schools, Porur, Chennai 4|Page


8. Write SQL queries for (1) to (7) and write the output for the SQL queries on the basis of table
ITEMS and TRADERS:

1. To display the details of all the items in ascending order of item names (i.e., INAME).
2. To display item name and price of all those items, whose price is in the range of 10000 and
22000 (both values inclusive).
3. To display the number of items, which are traded by each trader. The expected output of this
query should be:
4. To display the price, item name and quantity (i.e., qty) of those items which have quantity more
than 150.
5. To display the names of those traders, who are either from DELHI or from MUMBAI.
6. To display the names of the companies and the names of the items in descending order of
company names.
7. Obtain the outputs of the following SQL queries based on the data given in tables ITEMS and
TRADERS above.
o SELECT MAX (PRICE), MIN (PRICE) FROM ITEMS;
o SELECT PRICE*QTY FROM ITEMS WHERE CODE-1004;
o SELECT DISTINCT TCODE FROM ITEMS;
o SELECT INAME, TNAME FROM ITEMS I, TRADERS T WHERE [Link]=[Link] AND QTY< 100;
9. Write SQL queries for (a) to (f) and write the outputs for the SQL queries mentioned shown in
(g) parts on the basis of tables PRODUCTS and SUPPLIERS

a) To display the details of all the products in ascending order of product names (i.e., PNAME).
b) To display product name and price of all those products, whose price is in the range of 10000
and 15000 (both values inclusive).

© Swamy’s Group of Schools, Porur, Chennai 5|Page


c) To display the number of products, which are supplied by each supplier. i.e., the expected
output should be;
S01 2
S02 2
S03 1
d) To display the price, product name and quantity (i.e., qty) of those products which have quantity
more than 100.
e) To display the names of those suppliers, who are either from DELHI or from CHENNAI.
f) To display the name of the companies and the name of the products in descending order of
company names.
g) Obtain the outputs of the following SQL queries based on the data given in tables PRODUCTS
and SUPPLIERS above.
a. SELECT DISTINCT SUPCODE FROM PRODUCTS;
b. SELEC MAX (PRICE), MIN (PRICE) FROM PRODUCTS;
c. SELECT PRICE*QTY FROM PRODUCTS WHERE PID = 104;
d. SELECT PNAME, SNAME FROM PRODUCTS P, SUPPLIERS S
WHERE E SUPCODE = S. SUPCODE AND QTY>100;
10. Consider the following tables CARDEN and CUSTOMER and answer (b) and (c) parts of this
question:

1. Write SQL commands for the following statements:


o To display the names of all the silver-coloured cars.
o To display names of car, make and capacity of cars in descending order of their sitting
capacity.
o To display the highest charges at which a vehicle can be hired from CARDEN.
o To display the customer name and the corresponding name of the cars hired by them.
2. Give the output of the following SQL queries:
o SELECT COUNT(DISTINCT Make) FROM CARDEN;
o SELECT MAX(Charges), MIN (Charges) FROM CARDEN;
o SELECT COUNTS), Make FROM CARDEN;

© Swamy’s Group of Schools, Porur, Chennai 6|Page

Common questions

Powered by AI

SQL queries ensure data integrity in summarization using aggregate functions like AVG or SUM by grouping data appropriately with 'GROUP BY' and restricting inputs with 'WHERE' and 'HAVING' clauses to focus only on relevant data segments. This prevents skewing of results due to outliers or irrelevant entries. For example, 'SELECT AVG(QTY) FROM CUSTOMER WHERE NAME LIKE “%r%;' leverages WHERE to maintain accuracy by averaging only quantities where names match a pattern, thus ensuring meaningful and accurate summary statistics.

The DISTINCT keyword in SQL is used to return only distinct (different) values, which helps in removing duplicates from a result set. Although beneficial in ensuring data uniqueness, it can introduce challenges such as increased query execution time due to the additional processing needed to identify and remove duplicates. For example, 'SELECT DISTINCT TCODE from TRIP;' . It aids in achieving a cleaner dataset, particularly important in reporting and analytics, but may also burden the database with extra computational work, impacting performance.

The 'GROUP BY' clause in SQL is essential for aggregating records into groups of rows with the same values in specified columns, allowing you to perform operations such as COUNT, SUM, AVG, MIN, or MAX on each group. Without 'GROUP BY', aggregate functions would operate on the complete data set, losing the detail within grouped categories. For example, in the query 'SELECT COUNT(*), GENDER FROM ADMIN GROUP BY GENDER;' , 'GROUP BY GENDER' groups the entries by each gender, enabling the COUNT function to calculate the count of entries for each gender separately.

SQL joins combine rows from two or more tables based on a related column. They are crucial for comparing and retrieving related data that is spread across different tables. An example is 'SELECT A.TCODE, NAME, TTYPE FROM TRIP A, TRANSPORT B WHERE A.TCODE = B.TCODE AND KM < 90;' . This query uses an inner join to merge rows from 'TRIP' and 'TRANSPORT' tables where 'TCODE' match, allowing for combined data retrieval of transport codes, names, and types when kilometers traveled are less than 90.

Arithmetic operations in SQL SELECT statements enhance data analysis by transforming raw data into meaningful metrics, facilitating real-time calculations and insights without requiring data extraction to external tools. For example, 'SELECT NAME, KM *PERKM FROM TRIP A, TRANSPORT B WHERE A. TCODE = B. TCODE AND A. TCODE = 105' calculates total travel costs directly in the query, providing immediate analytical insights on trip expenses per kilometer, thereby streamlining processes like cost assessment and budgeting.

The WHERE clause is crucial in SQL for filtering records based on specified conditions, thereby refining query results to include only those rows that satisfy given criteria. This reduces the amount of data returned, improving performance and accuracy of data retrieval. For instance, 'SELECT TEACHER FROM SCHOOL WHERE EXPERIENCE >12 ORDER BY TEACHER;' uses WHERE to select only those teachers with experience greater than 12 years, ensuring efficient result sets that meet specific analytical needs.

Indexing in SQL significantly improves the performance of data retrieval operations by providing quick access paths to queried data, reducing the time taken to scan the entire table. Especially beneficial in large databases, indexes on columns used in WHERE, JOIN, and ORDER BY clauses can drastically speed up query execution by allowing rapid location of the desired rows. For instance, repeated queries like 'SELECT NO, NAME, TDATE from TRIP in descending order of NO' could benefit from indexing 'NO', enhancing performance even though the document doesn't explicitly cover this.

SQL commands like 'ALTER TABLE' are instrumental in modifying and managing database structures by allowing the addition, deletion, or modification of columns in existing tables. For example, the command to add a new column is 'ALTER TABLE [table_name] ADD [new_column_name] [data_type];', which is mentioned implicitly in the task to add a column 'totalprice' to the 'customer' table . This capability enables the adaptation of database structures without losing existing data, catering to changing business requirements or storing additional data.

Choosing a primary key in a relational database requires considering uniqueness, stability, and minimalism to ensure efficient indexing and reference integrity. A primary key uniquely identifies each row, crucial for joins and exact data retrieval. Inefficient keys, such as those with excessive length or volatility (e.g., large text), can degrade performance and slow down query execution. Although not discussed directly in the document, the structuring seen in tables like 'SCHOOL' and 'ADMIN' implies the need for careful key selection to maintain database organization and efficiency.

Subqueries can be nested within a SQL statement to perform complex data retrieval operations by allowing the result of one query to be used as a condition in another. Benefits include simplifying complex operations into manageable parts, improving query structure and readability, and enabling calculations that involve multiple steps or compare datasets dynamically. Though not directly mentioned in the document, the concept aligns with queries like finding maximum experience or counting specific roles where subqueries could be utilized for logical segmenting.

© Swamy’s Group of Schools, Porur, Chennai  
 
 
 
 
 
1 | P a g e  
 
                                 Swamy’s School
© Swamy’s Group of Schools, Porur, Chennai  
 
 
 
 
 
2 | P a g e  
 
b) To display the NAME of the drivers from the table T
© Swamy’s Group of Schools, Porur, Chennai  
 
 
 
 
 
3 | P a g e  
 
 
Write SQL statements for the following: 
1. To displ
© Swamy’s Group of Schools, Porur, Chennai  
 
 
 
 
 
4 | P a g e  
 
6. Answer the questions (a) and (b) based on the follo
© Swamy’s Group of Schools, Porur, Chennai  
 
 
 
 
 
5 | P a g e  
 
8. Write SQL queries for (1) to (7) and write the outp
© Swamy’s Group of Schools, Porur, Chennai  
 
 
 
 
 
6 | P a g e  
 
c) To display the number of products, which are suppli

You might also like