SQL Part1
SQL Part1
Prepared by:-
Shweta Wadhera
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.
Select
DCL(Data Control Language)
Grant,Revoke
Introduction to: 7
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
Syntax :
describe table-name;
Desc table-name;
Example :-
desc student
16
Insert Command
Example 1 :-
INSERT INTO Student
(Rno, student_name, address)
VALUES (1005, ‘Anisha Chopra’, ‘A-6/95 JP Towers
Dwarka’ ) ;
17
Example2 :--
SELECT Command
The FROM clause lists the tables from which to obtain the
data.
Eg 1:
ALTER TABLE department
ADD ( Mob_no number(10) ) ;
Eg 2:
ALTER TABLE vendor_table
ADD ( tel_no number(8), fax_no number(15) ) ;
25
Modifying the structure of the Table
Eg :
ALTER TABLE vendor_table
MODIFY (fax_no varchar2(15) ) ;
26
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 27
Example 2:
UPDATE Department
SET no_of_emp = 200 , Dept = ‘IT’
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 28
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
31
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
37
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….. 39
SELECT ename,sal
FROM emp
WHERE sal NOT BETWEEN 1500 AND 2850
SELECT ename,job,hiredate
FROM emp
WHERE hiredate BETWEEN '20-FEB-1981' AND '01-MAY-1981'
ORDER BY hiredate
9) Write a query to display the name and department number of All
employees in departments 10 and 30 in alphabetical order by name. 50
use of OR …..
SELECT ename,deptno
FROM emp
WHERE deptno=10 OR deptno=30
ORDER BY ename
SELECT ename,sal
FROM emp
WHERE (sal>1500) AND (deptno=10 or deptno=30)
11 ) Write a query to display the name and hire date of
51
every employees who were hired in 1981.
Use of LIKE and ‘ %’…….
SELECT ename,hiredate
FROM emp
WHERE hiredate LIKE '%81'
SELECT ename,job
FROM emp
WHERE mgr IS NULL
52
Select ename
From emp
Where (ename LIKE ‘ %R%R%’ OR ename LIKE
‘%A%A%’ ) AND ( ( deptno =30) OR (mgr=7788) ) ;
53
Functions …….
Ex 2 :
SELECT AVG( DISTINCT sal ) as “ Average salary “
FROM emp ;
COUNT (expr)
Syntax COUNT( [DISTINCT | ALL ] expr ) ;
EX 1:
SELECT COUNT (jobs) “Count is “
FROM emp;
Ex 2 :
SELECT COUNT ( DISTINCT jobs) “Count without duplicates “
FROM emp ;
55
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 ;
60
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 ;
61
LOWER
UPPER
LENGTH
Ex :
SELECT LENGTH (‘ pretty’) as “ Length of str”
FROM dual ;
Length of str
------------------
6
63
LPAD
Ex :
SELECT LPAD( ‘Page1’ , 14, ‘ * ‘ ) “ Lpad output “
FROM dual ;
Lpad output
---------------------
********Page 1
64
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 ;
68
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
71
QC)
SELECT ename || ‘ earns ‘ || salary || ‘ monthly but wants ‘ ||
3 * salary as “ Dream Salary “
FROM emp ;
QE)
Subquery
QG)
SELECT eno , ename , salary
FROM emp
WHERE salary > ( SELECT AVG ( salary )
FROM emp ) ;
77
80
**Q F ) Query to display name , hiredate and day of the week on
which the employee started .
81
82
**Q F )