SQL Final PDF
SQL Final PDF
1
Retrieve all the contents of CLIENT_MASTER table:
2
PROFITPERCENT NUMBER 4,2
UNITMEASURE VARCHAR2 10
QTYONHAND NUMBER 8
REORDERVAL NUMBER 8
SELLPRICE NUMBER 8,2
COSTPRICE NUMBER 8,2
3
View the whole table after inserting data in PRODUCT_MASTER:
4
Create a table SALESMAN_MASTER with following column names:
5
Insert the following data in this table SALESMAN_MASTER:
SALES SALESMA ADDR ADDR CITY PINC STATE SAL TGTT YTDS REM
MANNO NNAME ESS1 ESS2 ODE AMT OGET ALES ARKS
S00001 AMAN A/14 WOR MU 4000 MAHAR 3000 100 50 GOO
LI MBA 02 ASHTRA D
I
S00002 OMKAR 65 NARI MU 4000 MAHAR 3000 200 100 GOO
MAN MBA 01 ASHTRA D
I
S00003 RAJ P-7 BAND MU 4000 MAHAR 3000 200 100 GOO
RA MBA 32 ASHTRA D
I
S00004 ASHISH A/5 JUHU MU 4000 MAHAR 3500 200 150 GOO
MBA 44 ASHTRA D
I
6
7
QUERIES(1):
Find out the names of all clients:
Retrieve the list of names, city and the state of all the clients:
8
List the various PRODUCTS available from the PRODUCT_MASTER:
9
Change the CITY of CLIENTNO „C0C005‟ to „BANGALORE‟:
10
Change the COSTPRICE of „TROUSERS‟ to Rs. 950.00:
Delete all salesman from the SALESMAN_MASTER whose salaries are equal to 3500:
Delete all products from PRODUCT_MASTER where the QTYONHAND is equal to 100:
Delete from CLIENT_MASTER where the column STATE holds the value „TAMIL NADU‟:
11
Add a column called „TELEPHONE‟ of data type „NUMBER” and SIZE=‟10‟ to the CLIENT_MASTER
table:
12
13
QUERIES(2):
Create a table CLIENT_MASTER with following column names:
14
Create a table PRODUCT_MASTER with following column names:
15
OVERS
P07965 DENIM 4 PIECE 100 40 350 250
SHIRTS
P07975 LYERA 5 PIECE 70 30 300 175
TOPS
P08865 SKIRTS 5 PIECE 75 30 450 300
16
Create a table SALESMAN_MASTER with following column names:
SALES SALESMA ADDR ADDR CITY PINC STATE SAL TGTT YTDS REM
MANNO NNAME ESS1 ESS2 ODE AMT OGET ALES ARKS
S00001 AMAN A/14 WOR MU 4000 MAHAR 3000 100 50 GOO
LI MBA 02 ASHTRA D
I
S00002 OMKAR 65 NARI MU 4000 MAHAR 3000 200 100 GOO
MAN MBA 01 ASHTRA D
17
I
S00003 RAJ P-7 BAND MU 4000 MAHAR 3000 200 100 GOO
RA MBA 32 ASHTRA D
I
S00004 ASHISH A/5 JUHU MU 4000 MAHAR 3500 200 150 GOO
MBA 44 ASHTRA D
I
18
And insert the following data in this table:
19
Create a table SALES_ORDER_DETAILS with following column names:
20
21
22
QUERIES(3):
List the names of all the clients having „a‟ as the second letter in their names.
List the clients who stay in a city whose first letter is „M‟.
List all the clients whose BalDue is greater than value 10000.
List all the information from the Sales_Order table for orders placed in the month of june.
23
List products whose selling price is greater than 500 and less than or equal to 750.
List products whose selling price is more than 500. Calculate a new selling price as, original selling
price*0.15. Rename the new column in the output of the above query as new_price.
List the names, city and state of clients who are not in the state of „Maharashtra‟.
24
Determine the maximum and minimum product prices. Rename the output as max_price and min_price
respectively.
Count the number of products having price less than or equal to 500.
List all the products whose QtyOnHand is less than reorder level.
List the order number and day on which clients placed their order.
List the month (in alphabets) and date when the orders must be delivered.
25
List the OrderDate in the format „DD-Month-YY‟.
26
VIEWS & INDEXES:
Views
A view is like a “virtual” table
Defined by a query, which describes how to compute the view contents on the fly DBMS stores the
view definition query instead of view contents
Can be used in queries just like a regular table
Creating Views:
Syntax:
CREATE VIEW <VIEW NAME> AS SELECT <COLUMNNAME1>,<COLUMNNAME2> FROM
<TABLENAME> WHERE <COLUMNNAME>=<EXPRESSION LIST>;
GROUP BY <GROUPING CRITERIA> HAVING <PREDICATE>
For Example:
Modifying views:
Does not seem to make sense since views are virtual
But does make sense if that is how users see the database
Goal: modify the base tables such that the modification would appear to have been accomplished on
the view.
Destroying a View:
Syntax:
DROP VIEW <VIEW NAME>;
For Example:
Updateable views:
More or less just single-table selection queries
No join
No aggregation
No subqueries
27
Arguably somewhat restrictive
Still might get it wrong in some cases
See the slide titled “An impossible case”
Adding WITH CHECK OPTION to the end of the view
Indexes:
An index is an order list of content of a column,(or a group of columns) of a table.Indexing involves
forming a two dimensional matrices completely independent of table on which index is being created.
This two dimensional matrix will have a single column,which will hold sorted data,extracted from
table columns on which the index is created.
Another column called the address field identifies the location of record in oracle database.
The records in the index are sorted in ascending order of index column.
Creation of an index:
An index can be created in one or more columns.Based on the No. of columns included in the index, an index
can be:
Simple Index
Composite Index
For example:
For example:
28
For example:
Dropping Index:
Syntax:
DROP INDEX <INDEX NAME> ;
For example:
29
PL/SQL:
1) Write a PL/SQL code block that will accept an account no from user,check if user balance is less than
the minimum balance,only then deduct RS.100 from the balance.The process is fired on AM_MCA55
table.
30
2) Create a simple loop such that a message is displayed when a loop exceed a particular value.
3) Write a PL/SQL code block to calculate area of circle for a value of radius varying from 3 to 7.Store
the radius and corresponding values of calculated area in an empty table named area_mca52,consisting
of two columns radius and area.
31
4) Write a PL/SQL block of code for inverting a number 5639 to 9365.
32