0% found this document useful (0 votes)
19 views8 pages

SQL Assignment for Computer Science XII

The document is an SQL assignment for Grade XII Computer Science, detailing various SQL concepts and commands. It includes solved questions about SQL definitions, clauses, constraints, and practical queries related to database operations. The assignment covers topics such as DDL and DML statements, primary keys, joins, and examples of SQL commands for data manipulation and retrieval.

Uploaded by

sreyaguptha08
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)
19 views8 pages

SQL Assignment for Computer Science XII

The document is an SQL assignment for Grade XII Computer Science, detailing various SQL concepts and commands. It includes solved questions about SQL definitions, clauses, constraints, and practical queries related to database operations. The assignment covers topics such as DDL and DML statements, primary keys, joins, and examples of SQL commands for data manipulation and retrieval.

Uploaded by

sreyaguptha08
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

SUBJECT: COMPUTER SCIENCE

GRADE : XII
SQL – ASSIGNMENT

SOLVED QUESTIONS
1. What is SQL?
Structured Query Language (SQL) is a language used for accessing and manipulating
databases.
2. Define the following terms: Field, Record, Table.
o Field: A field is the smallest unit of a table which is also known as a column.
Columns are called attributes in a table. For example, Employee_Name,
Employee_ID, etc.
o Record: A record is a collection of values/fields of a specific entity. For example,
record of an employee that consists of Employee name, Salary, etc.
o Table: A table is called a relation. It is a collection of records of a specific type of
data. For example, employee table, salary table, etc., that can consist of the records
of employees and records of salary respectively.
3. What are DDL and DML statements?
DDL statements are used for creating or deleting tables, views, etc. DML statements are
used for manipulating values for records in a table.
4. In SQL, name the clause that is used to display the tuples in ascending order of an
attribute.
Ans. ORDER BY
5. What is NULL value?
A NULL value in a table is a value in a field which is blank. This means that a field with a
NULL value is a field with no value; not even zero is entered.
6. Differentiate between WHERE and HAVING clause.
WHERE clause is used to select particular rows that satisfy a condition whereas HAVING
clause is used to place condition on an individual group formed with GROUP BY clause.
For example:
SELECT * FROM Student WHERE Marks > 75;
This statement shall display the records for all the students who have scored more than 75
marks.
On the contrary, the statement —
SELECT * FROM Student GROUP BY Stream HAVING Marks > 75;
shall display the records of all the students grouped together on the basis of stream but
only for those students who have scored marks more than 75.
7. What is a NOT NULL constraint?
By default, we can add NULL values to the table columns until and unless NOT NULL
constraint has been explicitly defined. If you do not want a column to have a NULL value,
then you need to define that column with NOT NULL constraint.
8. What is the purpose of GROUP BY clause?
GROUP BY clause is used in a SELECT statement in combination with aggregate
functions to group the result based on distinct values in a column.
9. What is HAVING clause?
HAVING clause is used in combination with GROUP BY clause in a SELECT statement
to put condition on groups.

;
10. (a) What do you mean by Primary Key? Give a suitable example of a Primary Key from a
table containing some meaningful data.
Ans. An attribute or a set of attributes which is used to identify a tuple (row) uniquely is known
as Primary Key.
Table: Students
Admission_No First Name Last Name DOB
27354 Jatin Kumar 05-02-1998
25135 Mona Sinha 24-09-2004
25350 George Moun 19-05-1997
26385 Mukesh Kumar 24-09-2004

Admission_No is the Primary Key because this column will contain unique data for each record.

(b) Write a query that displays city, salesman name, code, and commission from salesman table.

(c) Write a query that selects all orders except those with zero or value as NULL in the amount
field from table orders.

(d) Write a command that deletes all orders for the customer name SOHAN from table customer.

(e) Differentiate between DROP and DELETE command.


[Link]. DROP command DELETE command
Drop command is used to delete a table Delete command is used to delete all the records
1 structure along with all the records or some records from a table without deleting the
stored in it. table.
It is a DDL (Data Definition Language) It is a DML (Data Manipulation Language)
2
command. command.
It frees the memory occupied by the The memory occupied by the table is not free
3
table. even if we delete all the rows of the table.

(f) How can you add a new column in a table?


Ans. If you want to add a new column in an existing table, ALTER TABLE command is used. For
example, to add a column bonus in a table emp, the statement will be given as:
ALTER TABLE Emp ADD bonus Integer;

(g) Can you add more than one column in a table by using the ALTER TABLE command?
Ans. Yes, we can add more than one column by using the ALTER TABLE command. Multiple
column names along with data types are given, which are separated by commas, while giving
with ADD clause. For example, to add city and pin code in a table employee, the command can
be given as:
ALTER TABLE Employee
ADD City CHAR(30), PINCODE INTEGER;

11. What are JOINS?


Ans. A JOIN is a query that combines tuples from more than one table. In a join query, the table
names are given with FROM clause, separated by a comma.
For example,
SELECT Name, Salary FROM Emp1, Emp2;
In this statement, the two tables are Emp1 and Emp2 from which the column name and salary are
extracted.

12. Differentiate between Alter and Update command.


Ans.
Alter Command
 It is a DDL command.
 It is also used to add or delete the constraints on an existing table.
Update Command
 It is used to change records of the table specified by a condition.
 It is a DML command.

11. How can you eliminate duplicate records in a table with SELECT query?
Ans. The DISTINCT clause is used with SELECT statement to hide duplicate records in a table.
For example, to display cities from table Suppliers:
SELECT DISTINCT City FROM Suppliers;

13. Consider a database LOANS with the following table:


Table: LOANS
AccNo Cust Name Loan Amount Instalments Int_Rate Start_Date Interest
1 R.K. Gupta 300000 36 12.00 2009-07-18 1200
2 S.P. Sharma 500000 48 10.00 2008-03-22 1800
3 K.P. Jain 300000 36 NULL 2007-03-08 1600
4 M.P. Yadav 800000 60 12.00 2010-12-06 2250
5 S.P. Sinha 200000 36 12.50 2008-01-03 4500
6 P. Sharma 1000000 60 12.50 2010-06-03 3600
7 K.S. Dhali 500000 48 NULL 2008-03-05 3800

Answer the following questions:


(i) Display the sum of all Loan Amounts whose Interest rate is greater than 10.
(ii) Display the Maximum Interest from Loans table.
(iii) Display the count of all loan holders whose name ends with ‘Sharma’.
(iv) Display the count of all loan holders whose Interest rate is NULL.
(v) Display the Interest-wise details of Loan Account Holders.
(vi) Display the Interest-wise details of Loan Account Holders with at least 10 instalments
remaining.
14.. Consider the table ‘HOTEL’ given below:

EMPID CATEGORY SALARY


E101 MANAGER 60000
E102 EXECUTIVE 65000
E103 CLERK 40000
E104 MANAGER 62000
E105 EXECUTIVE 50000
E106 CLERK 35000
Mr. Vinay wanted to display average salary of each category. He entered the following SQL
statement. Identify error(s) and rewrite the correct SQL statement:
SELECT CATEGORY, AVERAGE(SALARY) FROM HOTEL GROUP BY CATEGORY;

15. . What is an ORDER BY clause and GROUP BY clause?


Ans. ORDER BY clause is used to display the result of a query in a specific order (sorted order).
The sorting can be done in ascending or descending order. However, the actual data in the
database is not sorted but only the results of the query are displayed in sorted order. If order is not
specified then, by default, the sorting will be performed in ascending order.
For example,
SELECT Name, City FROM Student ORDER BY Name;
The above query returns name and city columns of table student sorted by name in ascending
order.
SELECT * FROM Student ORDER BY City DESC;
It displays all the records of table student ordered by city in descending order.

GROUP BY clause can be used in a SELECT statement to collect data across multiple records
and group the results by one or more columns.
For example,
SELECT City, Name, COUNT(*) AS "Number of employees"
FROM Employee WHERE Salary > 35000
GROUP BY City;
It displays name and the total number of employees of each city who are getting salary greater
than 35000.

16.. Consider the following tables Product and Client. Write SQL commands for the statements
(i) to (iii) and give outputs for SQL queries (iv) to (vi).
Table: PRODUCT

P_ID ProductName Manufacturer Price


TP01 Talcum Powder LAK 40
FW05 Face Wash ABC 45
BS01 Bath Soap ABC 55
SH06 Shampoo XYZ 120
FW12 Face Wash XYZ 95
Table: CLIENT
C_ID ClientName City P_ID
01 Cosmetic Shop Delhi FW05
02 Total Health Mumbai BS01
12 Live Life Delhi SH06
15 Pretty Woman Delhi FW12
16 Dreams Bengaluru TP01
(i) To display the details of those Clients whose city is Delhi.
(ii) To display the details of Products whose Price is in the range of 50 to 100 (both values
included).
(iii) To display the details of those products whose name ends with ‘Wash’.
(iv)
SELECT DISTINCT City FROM CLIENT;
(v)
SELECT Manufacturer, MAX(Price), MIN(Price), COUNT(*) FROM PRODUCT GROUP BY
Manufacturer;
(vi)
SELECT ProductName, Price * 4 FROM PRODUCT;

17.. Define a Foreign Key.


Ans. A foreign key is a key which is used to link two tables together. It is also called a
referencing key. Foreign key is a column or a combination of columns whose values match a
primary key in a different table. The relationship between two tables matches the primary key in
one of the tables with a foreign key in the second table. If a table has a primary key defined on
any field(s), then you cannot have two records having the same value of that field(s).

18.. Define the various SQL Constraints.


Ans. Constraints are the rules enforced on data or columns of a table. These are used to restrict
the values that can be inserted in a table. This ensures data accuracy and reliability in the
database.
Following are the most commonly used constraints available in SQL:
(a) NOT NULL Constraint: Ensures that a column cannot have NULL value.
(b) DEFAULT Constraint: Provides a default value for a column when no value is specified.
(c) UNIQUE Constraint: Ensures that all values in a column are unique. There should not be
any redundant value in a column which is being restricted.
(d) PRIMARY Key: Uniquely identifies each row/record in a database table.
(e) FOREIGN Key: Uniquely identifies a row/record in any other database table.
(f) CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy
certain conditions.
For example, to restrict the salary column that it should contain salary more than ₹10,000.
19. Consider the following tables: Company and Model.
Table: Company
Comp_ID CompName CompHO ContactPerson
1 Titan Okhla C.B. Ajit
2 Ajanta Najafgarh R. Mehta
3 Maxima Shahdara B. Kohli
4 Seiko Okhla R. Chadha
5 Ricoh Shahdara J. Kishore

Note:
→ Comp_ID is the Primary Key.

Table: Model
Model_ID Comp_ID Cost DateOfManufacture
T020 1 2000 2010-05-12
M032 4 7000 2009-04-15
M059 2 800 2009-09-23
A167 3 1200 2011-01-12
T024 1 1300 2009-10-14

Note:
 Model_ID is the Primary Key.
 Comp_ID is the Foreign Key referencing Comp_ID of the Company table.
Write SQL commands for queries (i) to (iv) and output for (v) and (vi):
(i) To display details of all models in the Model table in ascending order of DateOfManufacture.
(ii) To display details of those models manufactured in 2011 and whose cost is below 2000.
(iii) To display the Model_ID, Comp_ID, Cost from the Model table, and CompName and
ContactPerson from the Company table, with their corresponding Comp_ID.
(iv) To decrease the cost of all the models in Model table by 15%.
(v) SELECT COUNT(DISTINCT CompHO) FROM Company;
(vi) SELECT CompName, 'Mr.', ContactPerson FROM Company WHERE CompName LIKE
'M%';
Q.20(a) Explain the concept of Cartesian product between two tables with the help of example.
Note: Answer questions (b) and (c) on the basis of the following tables SHOP and
ACCESSORIES.
Table: SHOP
Id SName Area
S01 ABC Computronics CP
S02 All Infotech Media GK II
S03 Tech Shoppe CP
S04 Geek Tenco Soft Nehru Place
S05 Hitech Tech Store Nehru Place

Table: ACCESSORIES
No Name Price Id
A01 Motherboard 12000 S01
A02 Hard Disk 5000 S01
A03 Keyboard 300 S02
A04 Mouse 500 S01
A05 Motherboard 13000 S02
A06 Keyboard 400 S03
A07 LCD 5000 S04
A08 LCD 5500 S05
T09 Mouse 350 S05
T010 Hard Disk 450 S03

(b) Write SQL queries:


1. To display the Name and Price of all Accessories in ascending order of Price:
SELECT Name, Price FROM ACCESSORIES ORDER BY Price;
2. To display Id and Shop Name of all Shops located in Nehru Place:
SELECT Id, SName FROM SHOP WHERE Area = 'Nehru Place';
3. To display Maximum and Minimum Price of all Accessories:
SELECT MAX(Price), MIN(Price) FROM ACCESSORIES;

(c) Write the output of the following SQL commands:


1. SELECT DISTINCT Name FROM ACCESSORIES WHERE Price >= 5000;
2. SELECT Area, COUNT(*) FROM SHOP GROUP BY Area;

3. SELECT COUNT(DISTINCT Area) FROM SHOP;

21. (a) Define a Candidate Key with example: Write SQL queries for (b) to (f) and write the
output for the SQL queries mentioned in parts (g1) to (g3) on the basis of tables
PRODUCTS and SUPPLIERS.
(a) A table may have more than one such attribute/group of attributes that identifies a tuple
uniquely such attribute(s) is/are eligible for becoming a Primary key and is/are known as
Candidate keys.
For example, in the table item, columns into and icode are eligible for becoming a primary key
and thus can be classified as candidate keys.

Table: PRODUCTS

PID PName QTY PRICE COMPANY SUPCODE


101 DIGITAL CAMERA 14X 120 12000 RENIX S01
102 DIGITAL PAD 11i 100 22000 DIGI POP S02
104 PEN DRIVE 16 GB 500 1100 STOREKING S01
106 LED SCREEN 32 70 28000 DISPEXPERTS S02
105 CAR GPS SYSTEM 60 12000 MOVEON S03

Table: SUPPLIERS
SUPCODE SNAME CITY
S01 GET ALL INC KOLKATA
S03 EASY MARKET CORP DELHI
S02 DIGI BUSY GROUP CHENNAI

PID ICode Item QTY


103 P001 Pen 560
102 P001 Pencil 780
101 P002 CD 450
104 P001 Floppy 700
105 D001 Eraser 300
103 D001 Duster 200

(b) To arrange and display all the records of table Products on the basis of product name in the
ascending order:
(c) To display product name and price of all those products whose price is in the range of 10000
and 15000 (both values inclusive):
(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 names of the companies and the names 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:
(g1)
SELECT DISTINCT SUPCODE FROM PRODUCTS;
(g2)

SELECT MAX(PRICE), MIN(PRICE) FROM PRODUCTS;


(g3)
SELECT PRICE * QTY AS "AMOUNT" FROM PRODUCTS WHERE PID = 104;

You might also like