ch 9 structured query lang
ch 9 structured query lang
Question 1
1. DDL
2. DML
3. TCL
4. None of these
Answer
DDL
Reason — The DDL (Data Definition Language) commands allow us to perform tasks
related to data definition, i.e., related to the structure of the database objects.
Question 2
1. DDL
2. DML
3. TCL
4. None of these
Answer
DML
Reason — The DML (Data Manipulation Language) commands are used to manipulate
data, i.e., records or rows in a table or relation.
Question 3
1. Statement
2. Query
3. Comment
4. Clause
Answer
Comment
Reason — A comment is a text which is ignored by the SQL compiler and is not executed
at all. It is given for documentation purpose only.
Question 4
1. Keywords
2. Literal
3. Variable
4. Table
Answer
Keywords
Reason — A keyword refers to an individual SQL element that has a special meaning in
SQL.
Question 5
1. Use
2. Open
3. Distinct
4. Select
Answer
Use
Reason — The USE command is used to open the database for use. The syntax for
opening database is : USE <database_name>;.
Question 6
1. Use
2. Show
3. Fetch
4. Select
Answer
Select
Reason — The SELECT command is used to retrieve a subset of rows or columns from
one or more tables present in a database.
Question 7
The ............... keyword eliminates duplicate rows from the results of a SELECT statement.
1. Or
2. Distinct
3. Any
4. All
Answer
Distinct
Reason — The DISTINCT keyword in SQL is used to eliminate duplicate rows from the
results of a SELECT statement.
Question 8
1. Show
2. Select
3. Describe
4. Order by
Answer
Describe
Reason — To view a table structure, DESCRIBE or DESC command is used. It shows the
structure of the table along with the name of the columns, data type of the columns and
constraints applied on the columns.
Question 9
Which of the following types of table constraints will prevent the entry of duplicate rows?
1. Foreign Key
2. Distinct
3. Primary Key
4. NULL
Answer
Primary Key
Reason — A primary key is a set of one or more attributes or fields that uniquely identifies
a tuple or row in a table. Therefore, it ensures that each row in the table is unique and
prevents the entry of duplicate rows.
Question 10
1. desc databases;
2. show tables;
3. show databases;
4. describe databases;
Answer
show databases;
Reason — The SHOW DATABASES; command lists all the databases managed by the
MySQL server.
Question 11
(A) Delete
(B) Create
(C) Update
(D) Alter
(E) Drop
Answer
Reason — DDL (Data Definition Language) commands are used to create and define
tables and other database objects in SQL (Structured Query Language). DDL commands
such as CREATE, ALTER, and DROP, are used to create, define, change and delete
objects like tables, indexes, views, and constraints.
Question 12
Statement 2 (S2): Char data-type stores string smaller than the maximum field size.
Answer
Reason — The CHAR data type provides fixed-length memory storage. It specifies a
fixed-length character string. If the input string is shorter, MySQL pads it with spaces to fill
the fixed length. If the input string is longer, it is truncated to fit the fixed length.
Question 13
1. Unique
2. All
3. Order by
4. Distinct
Answer
Distinct
Reason — The DISTINCT keyword is used to display the unique values of the column in
MySQL.
Question 14
1. ,
2. :
3. ;
4. "
Answer
Question 15
Shivam wants to see the table structure in MySQL. Select an appropriate command to
help him.
1. Use
2. Show
3. Desc
4. Display
Answer
Desc
Question 16
Rajat wants to delete a primary key constraint from the table. Select an appropriate
command to do so.
1. Create
2. Alter
3. Drop
4. Delete
Answer
Alter
Reason — The ALTER TABLE command is used to delete a primary key constraint from
the table.
Question 17
Rajveer wants to rename column in display result for his query. Select the appropriate
query for the same:
Answer
Question 18
Answer
Question 20
Consider a table Student having two fields—FName varchar(20) and LName char(20). If
in a record, value stored in Fname is 'Anuj' and LName is 'Batra', then FName and LName
will consume ...............and ............... Character space, respectively.
1. 4, 5
2. 4, 20
3. 20, 4
4. 20, 20
Answer
4, 20
Reason — FName is a varchar(20) field, which means it can store a variable-length string
up to a maximum of 20 characters. Since the value stored in FName is 'Anuj', it will
consume 4 character spaces (A-n-u-j). LName is a char(20) field, which means it is a
fixed-length string that always occupies 20 character spaces, regardless of the actual
length of the string. Since the value stored in LName is 'Batra', it will still consume 20
character spaces, with the remaining 15 characters being padded with spaces.
Assertions and Reasons
Question 1
Reasoning (R): If a piece of data is stored in two places in the databases, then storage
space is wasted.
Answer
Question 2
Assertion (A): A database constraint can be added or removed any time in/from the
database tables.
Reasoning (R): Alter table command is used to change the structure of the table.
Answer
Explanation
A database constraint can be added or removed from database tables using the ALTER
TABLE command, even after the table has already been created. This command is used
to modify the structure of a table by altering the definition of its columns.
Question 3
Assertion (A): SQL has efficient mechanisms to retrieve data stored in multiple tables in
a MySQL database.
Reasoning (R): The SQL statement CREATE is used to retrieve data from the tables in a
database and is also called query statement.
Answer
Explanation
SQL provides efficient mechanisms, such as JOIN operations, to retrieve data from
multiple tables in a MySQL database. The SQL statement CREATE is used to create new
database objects such as tables, indexes, or views. The SELECT statement is used to
retrieve data from tables in a database and is known as a query statement.
Question 4
Assertion (A): The SQL keyword Like is used with wildcards only.
Reasoning (R): '_' underscore and "%" per cent are the two wildcard characters used
with LIKE clause.
Answer
Explanation
The SQL LIKE keyword allows the use of wildcard characters to perform pattern matching.
SQL provides two wildcard characters to use with the LIKE operator: the percent sign (%)
which matches any string, and the underscore ("_") which matches any single character.
Question 5
Answer
Explanation
The DISTINCT clause is used to remove duplicate rows from the results of
a SELECT statement. It retrieves only unique values for a column in the table.
The DISTINCT keyword in SQL can be used with any data type.
Question 6
Reasoning (R): SQL provides two keywords for sorting in ascending and descending
orders, ASC and DESC.
Answer
Explanation
The SQL ORDER BY clause is used to sort the data is ascending or descending order
based on one or more columns. This clause sorts the records in ascending order ( ASC) by
default. To sort the records in descending order, DESC keyword is used.
Question 7
Answer
Explanation
FLOAT and DOUBLE are data types in SQL, used to store decimal numbers. FLOAT can
store values with a precision of around 6-7 digits, while DOUBLE can store values with a
precision of around 15-16 digits.
Question 1
What is an Alternate Key?
Answer
A candidate key that is not the primary key is called an alternate key.
Question 2(a)
Define Relation.
Answer
Question 2(b)
Define tuple.
Answer
Question 2(c)
Define attribute.
Answer
Question 2(d)
Define domain.
Answer
A domain is a pool of values from which the actual values appearing in a given column are
drawn.
Question 3
What do you understand by the terms candidate key and alternate key in relational
database?
Answer
All attribute combinations inside a relation that can serve as primary key are candidate
keys as they are candidates for the primary key position.
A candidate key that is not the primary key is called an alternate key. In other words, any
attribute that is a candidate for the primary key, i.e., which is capable of becoming a
primary key but is not a primary key, is an alternate key.
Question 4
What is SQL? What are the different categories of commands available in SQL?
Answer
SQL (Structured Query Language) is a standard language for accessing and manipulating
databases.
Question 5
Answer
A Database Management System is a general purpose software system that facilitates the
process of defining, constructing and manipulating databases for various applications.
The database system is used to eliminate the problems of data redundancy and data
inconsistency.
Question 6
Answer
DDL provides a set of definitions to specify the storage structure and DML is a language that enables us
Data Definition Language (DDL) Data Manipulation Language
access methods used by the database system. access or manipulate data as organ
the appropriate data model.
DDL commands are used to perform tasks such as creating, altering, and
DML commands are used to retrie
dropping schema objects. They are also used to grant and revoke
insert, delete, modify data stored i
privileges and roles, as well as for maintenance commands related to
database.
tables.
Question 7
Answer
Data types are means to identify the type of data and associated operations for handling
it. The data types available in MySQL are int, float, date, time, char, varchar etc.
Question 8
Answer
Char datatype specifies a fixed length string. Varchar datatype specifies a variable length string.
Defining a length is not required, but the default is 1. Defining a length is required.
CHAR(n) ensures that all values stored in that VARCHAR(n) columns have a maximum size of n bytes,
column are of length n bytes, padding shorter values storing values exactly as specified without adding blanks for
with blanks while maintaining a fixed size of n bytes. shorter lengths. Exceeding n bytes results in an error message.
Question 9
Answer
Question 10
Answer
Question 11
Answer
Question 12
Write SQL queries to perform the following based on the table Product having fields as
(prod_id, prod_name, quantity, unit_rate, price, city)
(a) Display those records from table Product where prod_id is more than 100.
(c) List all those records whose price is between 200 and 500.
(a)
SELECT prod_name
FROM PRODUCT
WHERE quantity IS NULL;
(e)
Question 13(a)
Define Database.
Answer
Question 13(b)
Answer
Question 13(c)
Define Primary Key.
Answer
A primary key is a set of one or more attributes that can uniquely identify tuples within the
relation.
Question 13(d)
Answer
All attribute combinations inside a relation that can serve as primary key are candidate
keys as they are candidates for the primary key position.
Question 15
Answer
It can be used to add, modify, or drop columns, It is used to change the values of one or more columns in a table
constraints, or indexes in a table. based on specified conditions.
Question 16
Consider the following tables STORE and SUPPLIERS. Write SQL commands for the
statements (i) to (iii) and give the output for SQL query (iv).
Table: STORE
Rat
ItemNo Item Scode Qty LastBuy
e
Sharpener
2005 23 60 8 2009-06-31
Classic
Gel Pen
2002 21 150 12 2010-02-24
Premium
Gel Pen
2006 21 250 20 2009-03-11
Classic
Table: SUPPLIERS
Scode Sname
21 Premium Stationery
23 Soft Plastics
22 Tetra Supply
(ii) To display ItemNo and item name of those items from store table whose rate is more
than 15.
(iii) To display the details of those items whose supplier code is 22 or Quantity in store is
more than 110 from the table Store.
Answer
(i)
Output
+--------+-------------------+-------+-----+------+-----------
-+
| ItemNo | Item | Scode | Qty | Rate | LastBuy
|
+--------+-------------------+-------+-----+------+-----------
-+
| 2001 | Eraser Small | 22 | 220 | 6 | 2009-01-19
|
| 2002 | Gel Pen Premium | 21 | 150 | 12 | 2010-02-24
|
| 2003 | Ball Pen 0.25 | 22 | 50 | 25 | 2010-02-01
|
| 2004 | Eraser Big | 22 | 110 | 8 | 2009-12-02
|
| 2005 | Sharpener Classic | 23 | 60 | 8 | 2009-06-30
|
| 2006 | Gel Pen Classic | 21 | 250 | 20 | 2009-03-11
|
| 2009 | Ball Pen 0.5 | 21 | 180 | 18 | 2009-11-03
|
+--------+-------------------+-------+-----+------+-----------
-+
(ii)
Output
+--------+-----------------+
| ItemNo | Item |
+--------+-----------------+
| 2003 | Ball Pen 0.25 |
| 2006 | Gel Pen Classic |
| 2009 | Ball Pen 0.5 |
+--------+-----------------+
(iii)
Output
+--------+-----------------+-------+-----+------+------------+
| ItemNo | Item | Scode | Qty | Rate | LastBuy |
+--------+-----------------+-------+-----+------+------------+
| 2001 | Eraser Small | 22 | 220 | 6 | 2009-01-19 |
| 2002 | Gel Pen Premium | 21 | 150 | 12 | 2010-02-24 |
| 2003 | Ball Pen 0.25 | 22 | 50 | 25 | 2010-02-01 |
| 2004 | Eraser Big | 22 | 110 | 8 | 2009-12-02 |
| 2006 | Gel Pen Classic | 21 | 250 | 20 | 2009-03-11 |
| 2009 | Ball Pen 0.5 | 21 | 180 | 18 | 2009-11-03 |
+--------+-----------------+-------+-----+------+------------+
(iv) SELECT Rate*Qty FROM STORE WHERE Itemno = 2004;
Output
+------------+
| Rate * Qty |
+------------+
| 880 |
+------------+
Question 17
(i) Find the names of the employees with their dependents' names.
Answer
(i)
SELECT *
FROM EMPLOYEE
WHERE Department = 'PRODUCTION';
(iii)
SELECT e.Name
FROM EMPLOYEE e, DEPENDENT d
WHERE e.EmpID = d.EmpID AND d.DependentName IS NULL;
Question 18
Write SQL commands for (i) to (v) on the basis of relation given below:
Table: BOOKS
(ii) To display cost of all the books published for FIRST PUBL.
(iv) To display the Book_Name and price of the books more than 3 copies of which have
been issued.
(v) To show the details of the book with quantity more than 30.
Answer
(i)
SELECT *
FROM BOOKS
WHERE Publishers = 'FIRST PUBL.' AND author_name = 'J. Mukhi';
Output
+---------+-----------+-------------+-------------+--------+--
-------+-----+
| book_id | Book_name | author_name | Publishers | Price |
Type | qty |
+---------+-----------+-------------+-------------+--------+--
-------+-----+
| p0001 | Genuine | J. Mukhi | FIRST PUBL. | 755.00 |
Fiction | 24 |
+---------+-----------+-------------+-------------+--------+--
-------+-----+
(ii)
Output
+-----------+
| TotalCost |
+-----------+
| 1105.00 |
+-----------+
(iii)
UPDATE BOOKS
SET Price = Price - (Price * 0.05)
WHERE Publishers = 'EPB';
(iv)
Output
+-------------------------+--------+
| Book_name | Price |
+-------------------------+--------+
| Let us C | 427.50 |
| Programming with Python | 350.00 |
| Mastering C++ | 156.75 |
| VC++ advance | 250.00 |
| Genuine | 755.00 |
+-------------------------+--------+
(v)
SELECT *
FROM BOOKS
WHERE qty > 30;
Output
+---------+---------------+----------------+------------+-----
---+------+-----+
| book_id | Book_name | author_name | Publishers |
Price | Type | qty |
+---------+---------------+----------------+------------+-----
---+------+-----+
| m0001 | Mastering C++ | K.R. Venugopal | EPB |
156.75 | Comp | 60 |
| n0002 | VC++ advance | P. Purohit | TDH |
250.00 | Comp | 45 |
+---------+---------------+----------------+------------+-----
---+------+-----+
Question 19
Write SQL commands for (a) to (e) on the basis of PRODUCTS relation given below:
Table: PRODUCTS
(a) To show details of all PCs with stock more than 110.
(b) To list the company which gives warranty of more than 2 years.
(c) To find stock value of the BPL company where stock value is the sum of the products
of price and stock.
(e) To show the product name of the products which are within warranty as on date.
Answer
(a)
SELECT *
FROM PRODUCTS
WHERE PNAME = 'PC' AND STOCK > 110;
Output
+-------+-------+---------+-------+-------+-------------+-----
-----+
| PCODE | PNAME | COMPANY | PRICE | STOCK | MANUFACTURE |
WARRANTY |
+-------+-------+---------+-------+-------+-------------+-----
-----+
| P004 | PC | COMPAQ | 38000 | 120 | 2019-06-20 |
2 |
+-------+-------+---------+-------+-------+-------------+-----
-----+
(b)
Output
+---------+
| COMPANY |
+---------+
| BPL |
| SONY |
+---------+
(c)
Output
+---------+------------+
| COMPANY | StockValue |
+---------+------------+
| BPL | 2000000 |
+---------+------------+
(d)
Output
+-------+----------+---------+-------+-------+-------------+--
--------+
| PCODE | PNAME | COMPANY | PRICE | STOCK | MANUFACTURE |
WARRANTY |
+-------+----------+---------+-------+-------+-------------+--
--------+
| P001 | TV | BPL | 10000 | 200 | 2018-01-12 |
3 |
| P002 | TV | SONY | 12000 | 150 | 2017-03-23 |
4 |
| P003 | PC | LENOVO | 39000 | 100 | 2018-04-09 |
2 |
| P004 | PC | COMPAQ | 38000 | 120 | 2019-06-20 |
2 |
| P005 | HANDYCAM | SONY | 18000 | 250 | 2017-03-23 |
3 |
+-------+----------+---------+-------+-------+-------------+--
--------+
(e)
SELECT PNAME
FROM PRODUCTS
WHERE DATE_ADD(MANUFACTURE, INTERVAL WARRANTY YEAR) >=
CURDATE();
There is no output produced because the warranty of all products has expired.
Question 20
Answer
The Data Definition Language (DDL) part of SQL permits the creation or deletion of
database tables. It also defines indices (keys), specifies links between tables, and
imposes constraints on tables. DDL contains necessary statements for creating,
manipulating, altering, and deleting tables. Data Manipulation Language (DML) is a part of
SQL that helps users manipulate data. It contains necessary statements for inserting,
updating, and deleting data.
Question 21
Answer
A primary key is a set of one or more attributes/fields A candidate key refers to all the attributes in a relati
which uniquely identifies a tuple/row in a table. are candidates or are capable of becoming a primary
Primary key Candidate key
Question 22
What do you understand by the terms Cardinality and Degree of a relation in relational
database?
Answer
Question 23
Differentiate between DDL and DML. Mention the two commands for each category.
Answer
DDL commands are used to perform tasks such as creating, altering, and
DML commands are used to retrie
dropping schema objects. They are also used to grant and revoke
insert, delete, modify data stored i
privileges and roles, as well as for maintenance commands related to
database.
tables.
Question 24
Consider the given table and answer the questions.
Table: SCHOOLBUS
(a) To show all information of schoolbus where capacity is more than 70.
(b) To show area_covered for buses covering more than 20 km but charges less than
4000.
(d) To show Rtno, Area_Covered and Average cost per student for all routes where
average cost per student is—Charges/Noofstudents.
(a)
SELECT *
FROM SCHOOLBUS
WHERE CAPACITY > 70;
Output
+------+--------------+----------+--------------+----------+--
--------------+---------+
| Rtno | Area_Covered | Capacity | Noofstudents | Distance |
Transporter | Charges |
+------+--------------+----------+--------------+----------+--
--------------+---------+
| 1 | Vasant Kunj | 100 | 120 | 10 |
Shivam Travels | 3500.00 |
| 2 | Hauz Khas | 80 | 80 | 10 |
Anand Travels | 3000.00 |
| 4 | Rohini | 100 | 90 | 35 |
Anand Travels | 5000.00 |
| 7 | Vasundhara | 100 | 110 | 20 |
Yadav Travels | 3500.00 |
| 9 | Saket | 120 | 120 | 10 |
Speed Travels | 3500.00 |
| 10 | Janakpuri | 100 | 100 | 20 |
Kisan Tours | 3500.00 |
+------+--------------+----------+--------------+----------+--
--------------+---------+
(b)
SELECT AREA_COVERED
FROM SCHOOLBUS
WHERE DISTANCE > 20 AND CHARGES < 4000;
(c)
+----------------+---------------+
| Transporter | Total_Charges |
+----------------+---------------+
| Shivam Travels | 3500 |
| Anand Travels | 12500 |
| Bhalla Travels | 3800 |
| Yadav Travels | 7500 |
| Speed Travels | 6700 |
| Kisan Tours | 3500 |
+----------------+---------------+
(d)
Output
+------+---------------+--------------+
| RTNO | AREA_COVERED | AVERAEG_COST |
+------+---------------+--------------+
| 1 | Vasant Kunj | 29.166667 |
| 2 | Hauz Khas | 37.500000 |
| 3 | Pitampura | 81.818182 |
| 4 | Rohini | 55.555556 |
| 5 | Yamuna Vihar | 63.333333 |
| 6 | Krishna Nagar | 50.000000 |
| 7 | Vasundhara | 31.818182 |
| 8 | Paschim Vihar | 80.000000 |
| 9 | Saket | 29.166667 |
| 10 | Janakpuri | 35.000000 |
+------+---------------+--------------+
(e)
Question 25
Write SQL commands for (a) to (d) and write the output for (e) on the basis of the
following table:
Table: FURNITURE
(a) To list the details of furniture whose price is more than 10000.
(b) To list the Item name and Price of furniture whose discount is between 10 and 20.
Answer
(a)
Output
+----+-------------+-------------+-------------+-------+------
----+
| NO | ITEM | TYPE | DATEOFSTOCK | PRICE |
DISCOUNT |
+----+-------------+-------------+-------------+-------+------
----+
| 4 | Decent | OfficeTable | 2002-02-01 | 25000 |
30 |
| 5 | Comfortzone | DoubleBed | 2002-02-12 | 25000 |
30 |
+----+-------------+-------------+-------------+-------+------
----+
(b)
Output
+--------------+-------+
| ITEM | PRICE |
+--------------+-------+
| Pinkfeathers | 7000 |
| Dolphin | 9500 |
| Donald | 6500 |
+--------------+-------+
(c)
SELECT PRICE
FROM FURNITURE
WHERE TYPE = 'BabyCot';
Output
+-------+
| PRICE |
+-------+
| 7000 |
| 9500 |
| 6500 |
+-------+
(e)
Output
+-------------+
| Type |
+-------------+
| DoubleBed |
| BabyCot |
| OfficeTable |
+-------------+
Question 26
Write SQL commands for (a) to (d) and write the output for (e) and (f) on the basis of
given table GRADUATE:
Table: GRADUATE
(a) List the names of those students who have obtained rank 1.
(b) Display a list of all those names whose average is greater than 65.
(c) Display the names of those students who have opted computer as a subject with
average of more than 60.
Answer
(a)
Output
+--------+
| NAME |
+--------+
| KARAN |
| RAJ |
| DIVYA |
| GAURAV |
| VARUN |
| LIZA |
| PUJA |
+--------+
(b)
SELECT NAME
FROM GRADUATE
WHERE AVERAGE > 65;
Output
+--------+
| NAME |
+--------+
| KARAN |
| RAJ |
| GAURAV |
| LIZA |
+--------+
(c)
SELECT NAME
FROM GRADUATE
WHERE SUBJECT = 'COMPUTER' AND AVERAGE > 60;
Output
+------+
| NAME |
+------+
| LIZA |
+------+
(d)
SELECT name
FROM GRADUATE;
Output
+--------+
| name |
+--------+
| KARAN |
| RAJ |
| DEEP |
| DIVYA |
| GAURAV |
| MANAV |
| VARUN |
| LIZA |
| PUJA |
| NISHA |
+--------+
(e)
Output
+-------+--------+---------+---------+---------+------+
| S.No. | name | stipend | subject | average | RANK |
+-------+--------+---------+---------+---------+------+
| 1 | KARAN | 400 | PHYSICS | 68 | 1 |
| 5 | GAURAV | 500 | PHYSICS | 70 | 1 |
| 9 | PUJA | 500 | PHYSICS | 62 | 1 |
+-------+--------+---------+---------+---------+------+
(f) Since 'RANK' is a reserved keyword in SQL, we encounter an error while running this
query. To avoid such errors, we can enclose the column name 'RANK' in backticks to treat
it as a literal identifier.
Output
+------+
| RANK |
+------+
| 1 |
| 1 |
| 2 |
| 1 |
| 1 |
| 2 |
| 1 |
| 1 |
| 1 |
| 2 |
+------+
Question 27(a)
Answer
A candidate key refers to all the attributes in a relation that Any attribute which is capable of becoming a p
are candidates or are capable of becoming a primary key. key but is not a primary key is called an alterna
Question 27(b)
What is the degree and cardinality of a table having 10 rows and 5 columns?
Answer
Question 27(c)
Table:STUDENT
(ii) Write SQL query to increase the size of SNAME to hold 30 characters.
(iv) Write SQL query to insert a row in the table with any values of your choice that can be
accommodated there.
Answer
(i)
Question 28
Table: CLIENT
(i) Write SQL query to display ProductName and Price for all products whose Price is in
the range 50 to 150.
(ii) Write SQL Query to display details of products whose manufacturer is either XYZ or
ABC.
(iii) Write SQL query to display ProductName, Manufacturer and Price for all products that
are not given any discount.
(iv) Write SQL query to display ProductName and price for all products.
(v) Write SQL query to display ClientName, City, P_ID and ProductName for all clients
whose city is Delhi.
(vi) Which column is used as Foreign Key and name the table where it has been used as
Foreign Key?
Answer
(i)
Output
+-------------+-------+
| ProductName | Price |
+-------------+-------+
| Bath Soap | 55 |
| Face Wash | 95 |
| Shampoo | 120 |
+-------------+-------+
(ii)
Output
+------+-------------+--------------+-------+----------+
| P_ID | ProductName | Manufacturer | Price | Discount |
+------+-------------+--------------+-------+----------+
| BS01 | Bath Soap | ABC | 55 | NULL |
| FW05 | Face Wash | ABC | 45 | 5 |
| FW12 | Face Wash | XYZ | 95 | NULL |
| SH06 | Shampoo | XYZ | 120 | 10 |
+------+-------------+--------------+-------+----------+
(iii)
+---------------+--------------+-------+
| ProductName | Manufacturer | Price |
+---------------+--------------+-------+
| Bath Soap | ABC | 55 |
| Face Wash | XYZ | 95 |
| Talcum Powder | LAK | 40 |
+---------------+--------------+-------+
(iv)
Output
+---------------+--------+
| productname | price |
+---------------+--------+
| Bath Soap | 55.00 |
| Face Wash | 45.00 |
| Face Wash | 95.00 |
| Shampoo | 120.00 |
| Talcum Powder | 40.00 |
+---------------+--------+
(v)
Output
+---------------+-------+------+---------------+
| ClientName | City | P_ID | ProductName |
+---------------+-------+------+---------------+
| Cosmetic Shop | Delhi | TP01 | Talcum Powder |
| Live Life | Delhi | BS01 | Bath Soap |
| Pretty Woman | Delhi | SH06 | Shampoo |
| Dreams | Delhi | FW12 | Face Wash |
+---------------+-------+------+---------------+
(vi) The column used as a Foreign Key is P_ID in the CLIENT table, and it references the
P_ID column in the PRODUCT table.
Question 29
Table:HOSPITAL
(i) To list the names of all the patients admitted after 15/01/98.
(ii) To list the names of female patients who are in ENT department.
(iv) To display Patient’s Name, Charges and Age for only female patients.
Answer
(i)
SELECT NAME
FROM HOSPITAL
WHERE DATOFADM > '1998-01-15';
Output
+--------+
| NAME |
+--------+
| Arpit |
| Kareem |
| Ketaki |
| Ankita |
| Zareen |
| Shilpa |
+--------+
(ii)
SELECT NAME
FROM HOSPITAL
WHERE SEX = 'F' AND DEPARTMENT = 'ENT';
Output
+---------+
| NAME |
+---------+
| Zareena |
| Ketaki |
+---------+
(iii)
Output
+---------+------------+
| name | dateofadm |
+---------+------------+
| Arpit | 1998-01-21 |
| Zareena | 1997-12-12 |
| Kareem | 1998-02-19 |
| Arun | 1998-01-11 |
| Zubin | 1998-01-12 |
| Ketaki | 1998-02-24 |
| Ankit | 1998-02-20 |
| Zareen | 1998-02-22 |
| Kush | 1998-01-13 |
| Shilpa | 1998-02-21 |
+---------+------------+
(iv)
Output
+---------+---------+-----+
| NAME | CHARGES | AGE |
+---------+---------+-----+
| Zareena | 250 | 22 |
| Ketaki | 250 | 16 |
| Ankita | 800 | 29 |
| Zareen | 300 | 45 |
| Shilpa | 400 | 23 |
+---------+---------+-----+