Structured Query Language (SQL) : Prepared By:-Rahul
Structured Query Language (SQL) : Prepared By:-Rahul
Structured Query Language (SQL) : Prepared By:-Rahul
Prepared by:-
Rahul
2
DBMS LAB-1
Learning Objectives
Introduction to SQL
Concept of Table and Relation
Classification of SQL
Table Creation
Data Insertion
Data Retrieval
Table Structure
Modification
Data Deletion
Table Deletion
3
How to use SQL for data administration (to create tables etc.)
How to use SQL for data manipulation (to add, modify, delete
data)
SQL is the standard language for Relation Database System. All relational
database management systems like MySQL, MS Access, Oracle, Sybase,
Informix, and SQL Server uses SQL as standard database language.
Why SQL ? 5
sublanguages.
number(o, d): Numeric data type for integers and reals. o = overall
number of digits, d = number of digits to the right of the decimal point.
Maximum values: o =38, d= -84 to +127.
Examples: number(8), number(5,2)
Note that, e.g., number(5,2) cannot contain anything larger than 999.99
without resulting in an error. Data types derived from number are
int[eger], dec[imal], smallint and real.
date: Date data type for storing date and time. The default format for a
date is: DD-MMM-YY. Examples: '13-OCT-94', '07-JAN-98'
13
Example :-
CREATE TABLE student ( Rno number(10), student_name
varchar2(30), cgpa decimal(5,3),branch varchar2(10), mob_no
number(10) );
15
Example :-
16
Describe Command
Syntax :
describe table-name;
Desc table-name;
Example :-
desc student
17
Insert Command
Example 1 :-
INSERT INTO Student
(Rno, student_name, address)
VALUES (1005, ‘Anisha Chopra’, ‘A-6/95 JP Towers
Dwarka’ ) ;
18
Example2 :--
SELECT Command
The FROM clause lists the tables from which to obtain the
data.
Eg 1:
ALTER TABLE department
ADD ( no._of_emp number(4) ) ;
Eg 2:
ALTER TABLE vendor_table
ADD ( tel_no number(8), fax_no number(15) ) ;
26
Modifying the structure of the Table
Eg :
ALTER TABLE vendor_table
MODIFY (fax_no varchar2(15) ) ;
27
Updating the contents of a Table
Syntax:
UPDATE tablename
SET columnname = expression [, columname = expression
[WHERE columname = expression ] ;
Example 1:
UPDATE Department
SET no_of_emp = 50
WHERE Dno = 10
Updating the contents of a Table 28
Example 2:
UPDATE Department
SET no_of_emp = 200
WHERE Dno = 30
Example 3:
UPDATE Vendor
SET vendor_ name = ‘Varsha Kapoor’ ,
address = ‘ A 8/97, J P Towers,
Dwarka’
WHERE vendor_no = 1005 ;
Deleting Table Rows 29
DROP COMMAND
DROP command will permanently delete the table with all its data
BETWEEN
Used to check whether attribute value is within a range
IS NULL
Used to check whether attribute value is null
LIKE
Used to check whether attribute value matches given string
pattern
IN
Used to check whether attribute value matches any value within
a value list
EXISTS
Used to check if subquery returns any rows
32
ALTER COMMAND
One can add a new column,drop an existing column,modify the
datatype of a column,and drop the constraints using the following
commands respectively.
Assignment Continued...
Populate the borrower realtion the following data
Customer_name loan_number
Adams L-16
Curry L-93
Hayes L-15
Jackson L-14
Jones L-17
Smith L-11
Smith L-23
Williams L-17
38
Assignment Continued...
Populate account relation with the following data
Assignment Continued...
Populate the depositor relation with the following data
Customer_name account_number
Hayes A-102
Johnson A-101
Johnson A-201
Jones A-217
Lindsay A-222
Smith A-215
Turner A-305
Create the following table….. 40
use of OR …..
49
10 ) Write a query to display the name and salary and Dept
no of employees who earnes more than $1500 and are in
dept no 10 or 30 .
Use of AND ……
Select ename
From emp
Where (ename LIKE ‘ %R%R%’ OR ename LIKE
‘%A%A%’ ) AND ( ( deptno =30) OR (mgr=7788) ) ;
51
Functions …….
Ex 2 :
SELECT AVG( DISTINCT sal ) as “ Average salary “
FROM emp ;
COUNT (expr)
Syntax COUNT( [DISTINCT | ALL ] expr ) ;
Ex 2 :
SELECT COUNT ( DISTINCT jobs) “Count without duplicates “
FROM emp ;
53
COUNT ( * )
Returns the num of rows in the Table , Including Duplicates
and those with NULL values.
MAX
Returns MAX value of expression .
Q J)
SELECT COUNT ( DISTINCT mgr )
FROM emp ;
Q K)
ABS
Returns the absolute value of n .
Ex :
SELECT ABS (-15) as “ Absolute value”
FROM dual ;
58
ROUND
Syntax ROUND ( n[,m] )
Returns n rounded to m places, right of the decimal point;
to 0 places if m is omitted .
Ex :
SELECT ROUND(15.19 , 1) “ Rounded val “
FROM dual ;
SQRT
Syntax SQRT( n )
Returns square root of n; NULL if n<0 .
Ex :
SELECT SQRT (25) “ Square root”
FROM dual ;
59
LOWER
UPPER
LENGTH
Ex :
SELECT LENGTH (‘ pretty’) as “ Length of str”
FROM dual ;
Length of str
------------------
6
61
LPAD
Ex :
SELECT LPAD( ‘Page1’ , 14, ‘ * ‘ ) “ Lpad output “
FROM dual ;
Lpad output
---------------------
********Page 1
62
RPAD
Syntax : RPAD (char1 , n , [ char2 ] )
Ex :
SELECT RPAD( ename , 10, ‘ x ‘ ) “ ename as Rightpadded
output “
FROM emp
WHERE ename = ‘ PRIYA’ ;
SELECT SYSDATE
FROM dual ;
66
QA)
QB)
SELECT ename,
ROUND ( MONTHS_BETWEEN ( (Select SYSDATE from dual ) ,
hiredate ) ) as “ Num_of_Months “
FROM emp ; Imbedded Query
which returns Date
type value
Date type Value
69
QC)
SELECT ename || ‘ earns ‘ || salary || ‘ monthly but wants ‘ ||
3 * salary as “ Dream Salary “
FROM emp ;
QE)
**Q F )
Subquery
QG)
SELECT eno , ename , salary
FROM emp
WHERE salary > ( SELECT AVG ( salary )
FROM emp ) ;
76
79