PRACTICAL-1
Aim: Creation of some sample table in DBMS
1. TABLE NAME: Client_master
DESCRIPTION: Used to store information about clients.
Column Data Type Size
Name
Client_no Varchar2 6
Name Varchar2 20
Address1 Varchar2 30
Address2 Varchar2 30
City Varchar2 15
Pincode Number 8
State Varchar2 15
Bal_due Number 10,2
2. TABLE NAME: Product_master
DESCRIPTION: Used to store information about products.
Column Name Data Type Size
Product_no Varchar2 6
Description Varchar2 15
Profit_percent Number 4,2
Unit_measure Varchar2 10
Qty_on_hand Number 8
Reorder_lvl Number 8
Sell_price Number 8,2
Cost_price Number 8,2
3. TABLE NAME: Salesman_master
DESCRIPTION: Used to store information about salesmen in
company.
Column Name Data Type Size
Salesman_no Varchacr2 6
Salesman_nam Varchacr2 20
e
Address1 Varchacr2 30
Address2 Varchacr2 30
City Varchacr2 20
Pincode Number 8
State Varchacr2 20
Sal_amt Number 8,2
Tgt_to_get Number 6,2
Ytd_sales Number 6,2
Remark Varchacr2 60
4. TABLE NAME: Staff_master
DESCRIPTION: Used to store information about staff in college.
Column Name Data Type Size
Staff_ID Varchacr2 6
Staff_name Varchacr2 20
Address1 Varchacr2 30
Address2 Varchacr2 30
City Varchacr2 20
Pincode Number 8
State Varchacr2 20
Staff_salary Number 8,2
Remark Varchacr2 60
5. TABLE NAME: College_master
DESCRIPTION: Used to store information about college.
Column Name Data Type Size
College_code Varchacr2 6
College_name Varchacr2 20
Address1 Varchacr2 30
Address2 Varchacr2 30
City Varchacr2 20
Pincode Number 8
State Varchacr2 20
QUERIES:
1. Create the above tables.
QUERIES ANSWERS:
1.1 TABLE NAME: client_master
DESCRIPTION: Used to store information about clients.
Create table client_master(client_no varchar2(6),name
varchar2(20),address1 varchar2(30),address2 varchar2(30),city
varchar2(15),pincode number(8), state varchar2(15),bal_due
number(10,2));
1.2. TABLE NAME: product_master
DESCRIPTION: Used to store information about products.
Create table product_master(product_no varchar2(6),description
varchar2(15),profit_percent number(4,2),unit_measure
varchar2(10),qty_on_hand number(10),reorder_lvl
number(8),sell_price number(8,2),cost_price number(8,2));
1.3. TABLE NAME: salesman_master
DESCRIPTION: Used to store information about salesmen in
company.
Create table salesman_master(salesman_no
number2(6),salesman_name varchar2 (20), address1 varchar2
(30),address2 varchar2(30),city varchar2(20),pincode
number(8),state varchar2(20),sal_amt number(8,2),tgt_to_get
number(6,2),ytd_sales number(6,2),remarks varchar2(60));
1.4. TABLE NAME: Staff_master
DESCRIPTION: Used to store information about staff in company.
Create table Staff_master(staff_ID_no number2(6),staff_name varchar2
(20), address1 varchar2 (30),address2 varchar2(30),city
varchar2(20),pincode number(8),state varchar2(20),staff_salary
number(8,2),remarks varchar2(60));
1.5. TABLE NAME: College_master
DESCRIPTION: Used to store information about college.
Create table salesman_master(College_code
number2(6),College_name varchar2 (20), address1 varchar2
(30),address2 varchar2(30),city varchar2(20),pincode
number(8),state varchar2(20));
PRACTICAL-2
Aim:Write an SQL query to create record and insert 10 records
From above you have created various types of Table like Client_Master,
Product_Master, Salesman_Master, etc and other tables. Now the
structure and basic format of the table is “ready”. We now need to “fill
the table” and start inserting the records.
INSERT INTO:
SYNTAX:
INSERT into tablename values (value list);
SQL> insert into Client_master values(‘C01’,’Akash’,’vinayak
nagar’,’behind yogeshwar soc’,’Vadodara’,397001,’Gujarat’,1000);
Output: 1 row created.
SQL> insert into Client_master values(‘C02’,’Shrikant’,’kalyan’,’near
garden’,’mumbai’,450001,’Maharashtra’,2000);
SQL> insert into Client_mastervalues (‘C03’,’Akshay’,’kedar
dham’,’tarsali,’vadodara’,360012,’Gujarat’,3000);
SQL> insert into Client_master values (‘C04’,’Ashima’,’hare
krishna’,’near garden’,’mumbai’,450001,’Maharashtra’,4000);
SQL> insert into Client_master values (‘C05’,’Vishal’,’shree hari
soc’,’tarsali’,’vadodara’,390001,’Gujarat’,5000);
SQL> insert into Client_master values
(‘C06’,’Vikram’,’vrundavan’,’near
garden’,’vadodara’,360001,’Gujarat’,6000);
SQL> insert into Client_master values
(‘C07’,’Anil’,’uma’,’kalaniketan’,’vadodara’,390001,’Gujarat’,7000);
SQL> insert into Client_master values (‘C08’,’Shruti’,’govardhan
nagar’,’near hari krupa soc’,’Godhra’,389001,’Gujarat’,8000);
SQL> insert into Client_master values (‘C09’,’Krupa’,’veer
soc’,’manjalpur’,’vadodara’,389019,’Gujarat’,9000);
SQL> insert into Client_master values (‘C10’,’Misha’,’hare
krishna’,’near garden’,’mumbai’,450001,’Maharashtra’,4000);
Similarly, you can add values and records in other tables also.
Different students try filling the records in different tables. As an
example we have inserted a few records in Table Client_Master. Try
inserting records in Product_master, Salesman_master,
College_master tables,etc as given above
PRACTICAL-3
Aim:Write an SQL query to UPDATE record on a specific field
BASIC SYNTAX:
UPDATE table_name SET column1=value1,column2=value2,….
WHERE column_name=old_value;
Q: Change the city of client_no ‘C04’ to ‘mumbai’
SQL> UPDATE Client_master SET city=’Ahmedabad’ WHERE
Client_no=’C04’;
Now check the update has happened or not with the following query
Retrieve the entire contents of the client_master table.
SQL>Select * from client_master;
PRACTICAL-4
Aim: Write an SQL query to DELETE a particular table
BASIC SYNTAX:
DROP TABLE tableName;
Meaning :
All records along with the structure (or schema) of the table will be
destroyed and deleted. Records cannot be recovered after drop
command.
Example : DROP TABLE Client_master;
PRACTICAL-5
Aim:Write SQL queries to use various date functions.
NOTE:- “DUAL” is dummy table provided by Oracle itself i.e it is
inbuild table with one row and one column with single value ‘x’.
VARIOUS DATE FUNCTIONS ARE AS FOLLOWS :---
1) MONTHS_BETWEEN
Purpose
MONTHS_BETWEEN returns the number of months between dates
date1 and date2. If date1 is later than date2, then the result is positive.
If date1 is earlier than date2, then the result is negative. If date1 and
date2 are either the same days of the month or both last days of months,
then the result is always an integer.
Otherwise Oracle Database calculates the fractional portion of
the result based on a 31-day month and considers the difference
in time components date1 and date2.
Examples
The following example calculates the months between two dates:
SELECT MONTHS_BETWEEN
(TO_DATE('02-02-1995','MM-DD-YYYY'),
TO_DATE('01-01-1995','MM-DD-YYYY') ) "Months"
FROM DUAL;
OUTPUT
Months
1.03225806451612903225806451612903225806
2)NEXT_DAY (date, day):- This command returns
the date of next week-day specified by day
relative to date.
example :--
SELECT NEXT_DAY('28-NOV-2023','THURSDAY')
FROM DUAL;
OUTPUT
NEXT_DAY('28-NOV-2023','
THURSDAY')
30-NOV-23
3)LAST_DAY (date) : This command returns the
last date of the month specified by date.
SELECT LAST_DAY('2-NOV-2023')
FROM DUAL;
OUTPUT…
LAST_DAY('2-NOV-
2023')
30-NOV-23
4) TO_CHAR (date, format)
Converts a DATE value date to a CHAR value, using the format
specified. A format must be a valid format. If format is omitted, the
default format ‘DD-MON-YY’ is used.
Example:
SELECT TO_CHAR(SYSDATE,'DD-MM-YYYY') FROM DUAL;
OUTPUT
TO_CHAR(SYSDATE,'DD-M
M-YYYY')
29-11-2023 (GIVES
SYSTEM DATE)
PRACTICAL-6
Aim: Write SQL queries to use various numeric functions.
Numeric Functions are also called Arithmetic
Functions.
1) ABS
Syntax: ABS(N)
purpose: ABS returns the absolute value of n
Examples
The following example returns the absolute
value of -1523 I.E negative number converted to
positive numbers.
SELECT ABS(-1523) "Absolute" FROM DUAL;
Absolute
----------
1523
2)SQRT ();
SYNTAX : SQRT (n)
Returns square root of ‘n’
SELECT SQRT(36),SQRT(81) FROM DUAL;
OUTPUT
SQRT(36) SQRT(81)
6 9
3) CEIL
Purpose
CEIL returns the largest integer greater
than or equal to n.
Examples
The following example returns the largest
integer greater than or equal to the number
specified:
SELECT CEIL(100.12) FROM DUAL;
OUTPUT
CEIL(100.12)
101
4)COS,SIN,TAN
COS (n), SIN (n), TAN (n)
Returns the trignometric cosine,sine and tangent of the value
specified as ‘n’, where ‘n’ is an angle in radius.
EXAMPLE:
SELECT COS(3.1415),sin(3.1415),TAN(3.1415) FROM DUAL;
OUTPUT
COS(3.1415) SIN(3.1415) TAN(3.1415)
-.999999995707 .0000926535896 -.000092653590058
65615228385130 60671440566174 37250782204346053
998706452114 10860459683 380346235313
PRACTICAL NUMBER - 7
AIM :- Write SQL queries to use various
character functions.
Characters are the strings related functions
like converting a string to lowercase, upper
case, finding length of strings, etc.
Constant Strings can be passed as arguments and
constant strings need to be enclosed within
single quotes.
Examples of Character functions…
1)LENGTH (str)
Returns the Length of the given string.
SELECT LENGTH(‘Parul’) FROM dual;
OUTPUT
LENGTH('PARUL')
5
2)LOWER (str)
Returns str with all letters in lowercase.
SELECT LOWER(‘Parul unIVERSITY’) FROM dual;
OUTPUT
LOWER('Parul unIVERSITY')
parul university
3)UPPER (str)
Returns str with all letters in uppercase.
SELECT UPPER(‘Parul unIVERSITY’) FROM dual;
OUTPUT
UPPER('Parul unIVERSITY')
PARUL UNIVERSITY
4)INITCAP (str)
Return str with the first letter of each word in
uppercase.
SELECT INITCAP(‘PARUL unIVERSITY of BAroda’)
FROM dual;
OUTPUT
INITCAP('PARULUNIVERSITYOFBARODA'
)
Parul University Of Baroda
5)SUBSTR(str,pos,length)
Returns a portion of str, beginning at position
- pos and going up to length characters.The
first position in the string is 1. The ‘length’
is the number of characters to extract
SELECT SUBSTR (‘SACHIN Ramesh tendulkar’,8,6)
FROM dual;
OUTPUT
SUBSTR('SACHINRAMESHTENDULKAR',8,
6)
Ramesh
PRACTICAL NUMBER - 8
AIM :- Write SQL queries to use various
operators.
Operators can be Arithmetic operators
like(+,-,*,/,() ), Relational operators like (=,
<, >, <=,>=, != ), Logical operators like
AND,OR,NOT, Range Searching Operators like
BETWEEN, Set Searching Operators like IN,
Character Operators like columnName LIKE
Pattern.
Example
1)ADDITION OPERATOR (arithmetic). Create a table
in ONE COMPILER with the following commands and
insert some records.
EXAMPLE
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL,
salary INTEGER
);
-- insert
INSERT INTO EMPLOYEE VALUES (0001, 'RAJ',
'Sales','10000');
INSERT INTO EMPLOYEE VALUES (0002, 'Dave',
'Accounting','20000');
INSERT INTO EMPLOYEE VALUES (0003, 'ALIA',
'Sales','15000');
INSERT INTO EMPLOYEE VALUES (0004, 'ROHIT',
'IT','10000');
INSERT INTO EMPLOYEE VALUES (0005, 'DIVYA',
'Accounting','20000');
INSERT INTO EMPLOYEE VALUES (0006, 'ASIF',
'Sales','15000');
SELECT * FROM EMPLOYEE;
empId name dept salary
1 RAJ Sales 10000
2 Dave Accounting 20000
3 ALIA Sales 15000
4 ROHIT IT 10000
5 DIVYA Accounting 20000
6 ASIF Sales 15000
EXAMPLE:
SELECT empId,name,salary "CURRENT SALARY",
(salary+salary*0.1)"NEW SALARY" from EMPLOYEE;
OUTPUT
empId name CURRENT SALARY NEW SALARY
1 RAJ 10000 11000.0
2 Dave 20000 22000.0
3 ALIA 15000 16500.0
4 ROHIT 10000 11000.0
5 DIVYA 20000 22000.0
6 ASIF 15000 16500.0
2)AND (Logical Operator)
The AND operator is used to combine two or more
conditions in WHERE and HAVING clauses.
When WHERE clause is used in SQL statements,
each row of a table is processed one by one. If
any roq satisfies all the conditions combined
using the AND operator, then it will be included
in the result set.
EXAMPLE
SELECT * FROM EMPLOYEE WHERE salary ='10000' and
dept = 'IT';
OUTPUT
empId name dept salary
4 ROHIT IT 10000
3) OR OPERATOR: (logical operator)
The OR operator is also used to combine two or
more conditions in WHERE and HAVING clauses.
When a WHERE clause is used in a SQL statement,
each row of a table is processed one by one. If
any row satisfies any condition combined using
the OR operator, then it will be included in the
result set.
OR - requires any one condition to be true to
consider the entire clause to be true.
EXAMPLE
SELECT * FROM EMPLOYEE WHERE salary ='20000' or
dept = 'IT';
OUTPUT
empId name dept salary
2 Dave Accounting 20000
4 ROHIT IT 10000
5 DIVYA Accounting 20000
4) BETWEEN Operator ( Range Searching Operator)
This operator is used to select data that belong
to some particular range
Syntax:
-columnName BETWEEN lower limit AND upper limit;
Select rows that contain values within the range
specified by lower and upper limits.(Both the
lower and upper limits are inclusive in range.
Can be used with numerical, character as well as
date data type.
EXAMPLE
SELECT * FROM EMPLOYEE WHERE salary BETWEEN
10000 AND 15000;
OUTPUT
empId name dept salary
1 RAJ Sales 10000
3 ALIA Sales 15000
4 ROHIT IT 10000
6 ASIF Sales 15000
5)IN OPERATOR (Set Searching Operator)
This operator is used to select data that
belongs to some particular set of values.
Syntax:
-columnName IN (value1,value2,....,valueN)
Select rows that contain any value given in a
set. This is similar to ‘=’. But, ‘=’ compares a
single value to another single value, while IN
compares a single value to a list (set) of
values provided with IN predicate.Can also be
used with numerical, character as well as date
date type.
Example:
SELECT * FROM EMPLOYEE WHERE dept IN
('Sales','IT');
OUTPUT
empId name dept salary
1 RAJ Sales 10000
3 ALIA Sales 15000
4 ROHIT IT 10000
6 ASIF Sales 15000
Above query can also be provided with using
multiple OR conditions, such as dept = ‘Sales’
OR dept = ‘IT' .But using IN is cleaner,
readable.
6)LIKE Operator (Pattern Matching Operator)
SYNTAX :
-columnName LIKE Pattern;
Selects rows that contain values (strings)
similar to a given pattern.This is similar to
‘=’.But, the ‘=’ operator compares for exact
matching. While LIKE compares for pattern
similarity.Can be used with character data type
i.e with strings.
EXAMPLE:
SELECT * FROM EMPLOYEE WHERE name LIKE 'R%';
OUTPUT :
mpId name dept salary
1 RAJ Sales 10000
4 ROHIT IT 10000
EXAMPLE:
SELECT * FROM EMPLOYEE WHERE name LIKE '_A%';
OUTPUT:
empId name dept salary
1 RAJ Sales 10000
2 Dave Accounting 20000
Above query gives all records with second
character as ‘A’.
PRACTICAL NUMBER - 09
AIM :- Write SQL queries to use various
conversion functions.
Conversion functions are used to convert value
from one data type to another. Oracle supports
the following conversion functions:
1.Converting Character To Number
SYNTAX : TO_NUMBER(str)
Converts CHAR or VARCHAR2 to NUMBER
Returns equivalent numeric value to ‘str’.
‘str’ must consist of 0-9, decimal point, and
‘-’ or ‘+’ sign.
EXAMPLE:
SELECT TO_NUMBER(‘12345.67’) FROM dual;
OUTPUT
12345.67
2.Converting Number to Character
SYNTAX : TO_CHAR(n,format)
Converts a numerical value n to a character data
type, using optional format.
A format should be a valid numeric format
consisting of ‘0’,’9’, and ‘,’.
EXAMPLE:
SELECT TO_CHAR(123456,’09,99,999’) FROM DUAL;
OUTPUT
01,23,456
3.Converting Date to Character
SYNTAX : TO_CHAR (date,format)
Converts a DATE value to a CHAR value, using
format.
A format must be a valid date format.
PRACTICAL NUMBER - 10
AIM :- Write SQL queries to use various group
functions.
Functions that operate on a set or rows (or
values) are called Aggregate or Group Functions.
These functions accept a set of rows i.e a
group, as an input. They return only a single
row or ‘one single value’ as an output.
Various aggregate or group functions are as
follows:
1.MAX (columnName)
Return maximum value for a given column.
SELECT MAX(salary) “Maximum Salary” FROM
Employee;
OUTPUT
Maximum Salary
20000
2. MIN (columnName)
Return minimum value for a given column.
SELECT MIN(salary) “MINIMUM Salary” FROM
Employee;
OUTPUT
MINIMUM Salary
10000
3.SUM
SUM([Distinct or All ] columnName)
Returns sum of all values for a given column.If
‘distinct’ is provided, then duplicate values
will be considered only once. By default ‘All’
is considered.
SELECT SUM(salary), SUM(Distinct salary) FROM
Employee;
SUM(salary) SUM(Distinct salary)
90000 45000
4.AVG
AVG([Distinct or All ] columnName)
Returns average of all values for a given
column.If ‘distinct’ is provided, then duplicate
values will be considered only once. By default
‘All’ is considered.
SELECT AVG(salary), AVG(Distinct salary) FROM
Employee;
OUTPUT
AUG(salary) AVG(Distinct salary)
15000 15000
5.COUNT(*)
Returns number of rows in a table including
duplicates and having null values.
EXAMPLE
SELECT COUNT(*) FROM Employee;
OUTPUT
COUNT(*)
6
PRACTICAL NUMBER - 11
AIM :- Write SQL queries using GROUP BY, HAVING
AND ORDER BY clause.
To understand this practical…..
Consider the following TABLE AND VALUES IN THE
TABLE:
CREATE TABLE ACCOUNTS (
acc_no INTEGER PRIMARY KEY,
bran_name TEXT NOT NULL,
balance INTEGER
);
INSERT INTO ACCOUNTS VALUES (01, 'vvn', 5000);
INSERT INTO ACCOUNTS VALUES (02, 'ksad', 6000);
INSERT INTO ACCOUNTS VALUES (03, 'anand',
37000);
INSERT INTO ACCOUNTS VALUES (04, 'ksad', 8000);
INSERT INTO ACCOUNTS VALUES (05, 'vvn', 6000);
select * from ACCOUNTS;
output
acc_no bran_name balance
1 vvn 5000
2 ksad 6000
3 anand 7000
4 ksad 8000
5 vvn 6000
1)ORDER BY
Syntax:
SELECT * FROM tableName ORDER BY
column1[ORDER],column2[ORDER],....,columnN[ORDER
];
Example
SELECT * FROM Accounts ORDER BY bran_name;
OUTPUT
acc_no bran_name balance
3 anand 7000
2 ksad 6000
4 ksad 8000
1 vvn 5000
5 vvn 6000
This statement retrieves data in a sorted
manner.By default, order is considered as an
Ascending order. To sort data in descending
order, it is necessary to specify DESC as an
order.When multiple columns are provided and
some rows contain the same data for each column,
then sorting is performed based on data of the
next column specified.
1)The GROUP BY and HAVING clauses are used to
group records of a table. They are parallel
to ORDER BY and WHERE clauses. The
difference is GROUP BY and HAVING clauses
act on a set of records, while ORDER BY and
WHERE clauses act on individual records.
Example
SYNTAX:
SELECT column1,column2,.....,Aggregate Function
(argument) FROM tableName
GROUP BY column1,column2,.....,columnN;
Example :
SELECT bran_name,sum(balance) ‘Total Balance’
FROM ACCOUNTS GROUP BY bran_name;
OUTPUT
bran_name Total Balance
vvn 11000
ksad 14000
anand 7000
The result of sum(balance) has been renamed as
“Total Balance”.Any column can be renamed in the
same manner.
Here the records having the same value for
column bran_name are grouped together into
separate groups, and total balance for each
group is calculated.
As there are three different kinds of branch
names available in the Accounts Table, all
records are distributed in three different
groups according to their branch names.
2. HAVING
Syntax
SELECT column1,column2,....,Aggregate
Function(argument) from tableName GROUP BY
column1,column2,...columnN HAVING condition;
Example Syntax
SELECT bran_name,sum (balance) “Total Balance”
FROM Account GROUP BY bran_name HAVING bran_name
= ‘vvn’;
OUTPUT
bran_name Total Balance
vvn 11000
Example
Display branch names and total balance for each
branch having total balance greater than 12000.
Example Syntax
SELECT bran_name, sum(balance) “Total Balance”
FROM Accounts GROUP BY bran_name HAVING
sum(balance) > 12000;
Output
bran_name Total Balance
ksad 14000
PRACTICAL NUMBER - 12
AIM :- Write SQL queries to create a TABLE.
SYNTAX:
CREATE TABLE Employee
(eid INTEGER,
ename TEXT,
birthdate date,
salary INTEGER,
city TEXT
);
OUTPUT
table created
PRACTICAL NUMBER - 13
AIM :- Write SQL queries to insert a value into
a TABLE.
INSERT INTO TRIALTABLE VALUES (0001, 'RAJ',
'2010-06-01','10000','BARODA');
SELECT * FROM TRIALTABLE;
OUTPUT
eid ename birthdate salary city
1 RAJ 2021-06-01 10000 BARODA
PRACTICAL NUMBER - 14
AIM :- Write SQL queries to show the record in
the TABLE.
Consider the employee table as created in
previous practical example
To show all records use
SELECT * FROM EMPLOYEE;
Output:
empId name dept salary
1 RAJ Sales 10000
2 Dave Accounting 20000
3 ALIA Sales 15000
4 ROHIT IT 10000
5 DIVYA Accounting 20000
6 ASIF Sales 15000
SELECT * FROM EMPLOYEE where name = 'ASIF';
Output
empId name dept salary
6 ASIF Sales 15000
PRACTICAL NUMBER - 15
AIM :- Write SQL queries to show one field of
the table
cONSIDER THE ABOVE TABLE OF EMPLOYEES ONLY.
SYNTAX
SELECT yourfield FROM yourtable;
EXAMPLE
SELECT name FROM EMPLOYEE;
OUTPUT
| NAME |
+-------+
| RAJ |
| Dave |
| ALIA |
| ROHIT |
| DIVYA |
| ASIF |
+-------+
2)Retrieve values from a specific field with a condition (e.g., where
another field equals a certain value):
SYNTAX:
SELECT your_field FROM your_table WHERE another_field = 'some_value';
EXAMPLE:
SELECT name FROM EMPLOYEE WHERE dept = 'Sales';
OUTPUT
| name |
+------+
| RAJ |
| ALIA |
| ASIF |
+------+
3)Retrieve values from a specific field with ordering (e.g., in ascending
order):
SYNTAX:
SELECT your field FROM your_table ORDER BY your_field ASC;
EXAMPLE:
SELECT salary FROM EMPLOYEE ORDER BY SALARY ASC;
OUTPUT
| salary |
+--------+
| 10000 |
| 10000 |
| 15000 |
| 15000 |
| 20000 |
| 20000 |
+--------+
PRACTICAL NUMBER - 16
AIM :- Write SQL queries to DELETE THE RECORD IN
the table
Assuming you have a table named your_table and
you want to delete a record based on a
condition:
(1)Delete a specific record based on a
condition:
SYNTAX
DELETE FROM your_table WHERE your_condition;
Replace your_condition with the condition that
identifies the record you want to delete.
For example:
Consider the previous table
CREATE TABLE ACCOUNTS (
acc_no INTEGER PRIMARY KEY,
bran_name TEXT NOT NULL,
balance INTEGER
);
INSERT INTO ACCOUNTS VALUES (01, 'vvn', 5000);
INSERT INTO ACCOUNTS VALUES (02, 'ksad', 6000);
INSERT INTO ACCOUNTS VALUES (03, 'anand', 7000);
INSERT INTO ACCOUNTS VALUES (04, 'ksad', 8000);
INSERT INTO ACCOUNTS VALUES (05, 'vvn', 6000);
select * from ACCOUNTS;
DELETE FROM ACCOUNTS WHERE id = 03;
OUTPUT
acc_no | bran_name | balance |
+--------+-----------+---------+
| 1 | vvn | 5000 |
| 2 | ksad | 6000 |
| 4 | ksad | 8000 |
| 5 | vvn | 6000 |
+--------+-----------+---------+
(2)Delete all records from a table (be cautious
with this):
DELETE FROM your_table;
This will delete all records in the specified
table. Use it with caution as it removes all
data.
Remember to replace your_table with the actual
name of your table and adjust the condition
based on your requirements.
Make sure to have a backup or be certain of your
conditions when using DELETE statements,
especially when deleting all records, as it
permanently removes data from the table.
PRACTICAL NUMBER - 17
AIM :- Write SQL queries to SHOW SOME RECORDS IN
the table.
Consider the employee table as created in
previous practical example
(1)To show all records use
SELECT * FROM EMPLOYEE;
Output:
empId name dept salary
1 RAJ Sales 10000
2 Dave Accounting 20000
3 ALIA Sales 15000
4 ROHIT IT 10000
5 DIVYA Accounting 20000
6 ASIF Sales 15000
(2)To show/display selected rows and all columns
SELECT * FROM EMPLOYEE where name = 'ASIF';
Output
empId name dept salary
6 ASIF Sales 15000
(3) Selected Columns,Selected Rows
SYNTAX
SELECT column1,column2,...columnN from TABLENAME
WHERE conditions;
EXAMPLE
SELECT name FROM employee WHERE salary>10000;
OUTPUT
| name |
+-------+
| Dave |
| ALIA |
| DIVYA |
| ASIF |
+-------+
(4) ELIMINATING DUPLICATION
The DISTINCT key-word removes duplicate rows
from the result. It can be used only with SELECT
statements
SYNTAX WITHOUT DISTINCT:
SELECT dept from EMPLOYEE;
OUTPUT (WITHOUT DISTINCT)
dept |
+------------+
| Sales |
| Accounting |
| Sales |
| IT |
| Accounting |
| Sales
SYNTAX WITH DISTINCT:
SELECT DISTINCT dept from EMPLOYEE;
OUTPUT (WITH DISTINCT)
| dept |
+------------+
| Sales |
| Accounting |
| IT |
+------------+
PRACTICAL NUMBER - 18
AIM :- Write SQL queries using SET OPERATOR IN
the table.
NOTE : SET OPERATOR WILL RUN IN LIVE ONLINE SQL
COMPILER AND NOT ON ONE COMPILER SQL DUE TO
COMMAND UNAVAILABILITY IN ONE COMIPLER SQL
A database can have more than one tables also.
Different tables can have some common
relationship between them also SET operators are
used to work on these multiple tables. The most
common SET operations are - UNION, INTERSECT AND
MINUS.
Consider the following tables:
create table my_brick_collection (
colour text,
shape text,
weight integer
);
create table your_brick_collection (
height integer,
width integer,
depth integer,
colour text,
shape text
);
insert into my_brick_collection values ( 'red',
'cube', 10 );
insert into my_brick_collection values ( 'blue',
'cuboid', 8 );
insert into my_brick_collection values (
'green', 'pyramid', 20 );
insert into my_brick_collection values (
'green', 'pyramid', 20 );
insert into my_brick_collection values ( null,
'cuboid', 20 );
insert into your_brick_collection values ( 2, 2,
2, 'red', 'cube' );
insert into your_brick_collection values ( 2, 2,
2, 'blue', 'cube' );
insert into your_brick_collection values ( 2, 2,
8, null, 'cuboid' );
TABLE : my_brick_collection
| colour | shape | weight |
+--------+---------+--------+
| red | cube | 10 |
| blue | cuboid | 8 |
| green | pyramid | 20 |
| green | pyramid | 20 |
| NULL | cuboid | 20 |
+--------+---------+--------+
TABLE : YOUR_BRICK_COLLECTION
| height | width | depth | colour | shape |
+--------+-------+-------+--------+--------+
| 2 | 2 | 2 | red | cube |
| 2 | 2 | 2 | blue | cube |
| 2 | 2 | 8 | NULL | cuboid |
+--------+-------+-------+--------+--------+
1)INTERSECT
syntax:
QUERY1 INTERSECT QUERY2
The output will contain those records which are
common to query1 and query2.The number of
columns, name of each column and their data
types in both the query need to be the same.
The output will contain sorted records in
ascending order according to columns present in
SELECT statement
EXAMPLE
select colour, shape from your_brick_collection
intersect
select colour, shape from my_brick_collection;
OUTPUT
COLOUR SHAPE
Red cube
- cuboid
2)MINUS (DIFFERENCE)
The minus set operator will give records or
selected field, present in one table and not in
another. The number of column, name of each
column, and their data types in both the query
need to be same.The output will change if the
order of both the queries are changed i.e tables
are interchanged.NULL value is also considered
while comparing records in both the queries.
EXAMPLE
select colour, shape from your_brick_collection
minus
select colour, shape from my_brick_collection;
OUTPUT
COLOUR SHAPE
blue cube
PRACTICAL NUMBER - 19
AIM :- Write SQL queries to retrieve data from
multiple tables.
unions GIVES all the record from query 1 and
query2, but the common record records will
appear only once.The number of column, name of
each column, and their data types in both the
query need to be same.NULL value is also
considered while comparing records in both the
queries.
Union
The union operator combines two or more tables into a single result set. To use it, the
tables must have the same number of columns with matching data types in each
position.
The brick collection tables have different columns. So combining these with select *
leads to an error:
select * from my_brick_collection
union
select * from your_brick_collection;
To resolve this, select the common columns. Here that's the colour and shape. So this
query returns a list of all the ( colour, shape ) values in the tables:
select colour, shape from my_brick_collection
union
select colour, shape from your_brick_collection;
OUTPUT
COLOUR SHAPE
blue cube
blue cuboid
green pyramid
red cube
- cuboid
Here in above, values which are common in both
the relations appear only once in output.
UNION ALL
The UNION ALL clause will cause to appear values
multiple times with repetition (including
duplicates).
EXAMPLE
You can do this by sticking all after union:
select colour, shape from my_brick_collection
union all
select colour, shape from your_brick_collection;
PRACTICAL NUMBER - 20
WRITE SQL QUERIES TO show all the records and
modify some data.