7/27/2019 Ddl & Dml Command
1/38
SQL
7/27/2019 Ddl & Dml Command
2/38
Data definition language
SQL includes commands to create
Database objects such as tables, indexes, and views
Commands to define access rights to those database
objects
Data manipulation language
Includes commands to insert, update, delete, andretrieve data within the database tables
7/27/2019 Ddl & Dml Command
3/38
SQL Data Types
7/27/2019 Ddl & Dml Command
4/38
SQL> create table product(p_price number(7,2));
Table created.
SQL> insert into values('&p_price);
Enter value for p_price: 1234567
old 1: insert into values('&p_price)
new 1: insert into values('1234567)ERROR:
ORA-01756: quoted string not properly terminated
7/27/2019 Ddl & Dml Command
5/38
SQL> insert into product values('&p_price');
Enter value for p_price: 1234567
old 1: insert into product values('&p_price')
new 1: insert into product values('1234567')
insert into product values('1234567')
*
ERROR at line 1:
ORA-01438: value larger than specified precision allows
for this column
7/27/2019 Ddl & Dml Command
6/38
SQL> /
Enter value for p_price: 12345
old 1: insert into product values('&p_price')
new 1: insert into product values('12345')
1 row created.
SQL> /
Enter value for p_price: 123.1234567
old 1: insert into product values('&p_price')
new 1: insert into productvalues('123.1234567')
1 row created.
SQL> /
Enter value for p_price: .123456
old 1: insert into product values('&p_price')new 1: insert into product values('.123456')
1 row created.
SQL> /
Enter value for p_price: +1234.12345
old 1: insert into product values('&p_price')
new 1: insert into product
values('+1234.12345')1 row created.
SQL> /
Enter value for p_price: -123.123456
old 1: insert into product values('&p_price')
new 1: insert into product values('-
123.123456')
1 row created.
//////////////output/////////////////////
SQL> select * from product;
P_PRICE
----------12345
123.12
.12
1234.12
-123.12
7/27/2019 Ddl & Dml Command
7/38
Number data typeSQL> create table product1(p_price number,p_cost
number(3));
Table created.
SQL> insert into product1 values(123456789,12);
SQL> /
1 row created.
/*********output**********/
SQL> select * from product1;
P_PRICE P_COST
---------------- --------
123456789 12
SQL> insert into product1 values(1234,123);
1 row created.
SQL> insert into product1 values(1234,1234)
2 ;
insert into product1 values(1234,1234)
*
ERROR at line 1:
ORA-01438: value larger than specified
precision allows for this column
7/27/2019 Ddl & Dml Command
8/38
DATE TIMEData type Description
DATE() A date. Format: DD-MM-YYYY
Note: The supported range is from '1000-01-01' to '9999-12-31'
DATETIME() *A date and time combination. Format: DD-MM-YYYY HH:MM:SS
Note: The supported range is from '1000-01-01 00:00:00' to '9999-12-
31 23:59:59'
TIMESTAMP() *A timestamp. TIMESTAMP values are stored as the number of
seconds since the Unix epoch ('1970-01-01 00:00:00' UTC). Format:
DD-MM-YYYY HH:MM:SSNote: The supported range is from '1970-01-01 00:00:01' UTC to
'2038-01-09 03:14:07' UTC
TIME() A time. Format: HH:MM:SS
Note: The supported range is from '-838:59:59' to '838:59:59'
*Even if DATETIME and TIMESTAMP return the same format, they work very differently. In an INSERT
or UPDATE query, the TIMESTAMP automatically set itself to the current date and time. TIMESTAMP
also accepts various formats, like YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD, or
YYMMDD.
7/27/2019 Ddl & Dml Command
9/38
DATESQL> create table employee(d_O_b date);
Table created.
SQL> insert into employee values('01-jan-99');1 row created.
SQL> insert into employee values('01-feb-1999');
SQL> /
1 row created.SQL> select * from employee;
D_O_B
-------------01-JAN-99
01-FEB-99
7/27/2019 Ddl & Dml Command
10/38
timestamp
SQL> create table employee4(e_name
varchar(20),s_date TIMESTAMP(7))SQL> insert into employee4 values('deep','17-jun-
87 02.22.22')
1 row created.E_NAME S_DATE
---------- -------------
Deep 17-JUN-87 02.22.22.0000000 AM
7/27/2019 Ddl & Dml Command
11/38
SQL> create table employee5(e_name varchar(20),s_dateTIMESTAMP(4))
SQL> /
Table created.
1* insert into employee5 values('deep','17-jun-87 12.22.22')
SQL> /1 row created.
E_NAME S_DATE
------------ ----------------
deep 17-JUN-87 12.22.22.0000 PM
7/27/2019 Ddl & Dml Command
12/38
MySQL Date FunctionsFunction Description
NOW() Returns the current date and time
CURDATE() Returns the current dateCURTIME() Returns the current time
DATE() Extracts the date part of a date or date/timeexpression
EXTRACT() Returns a single part of a date/time
DATE_ADD() Adds a specified time interval to a date
DATE_SUB() Subtracts a specified time interval from a date
DATEDIFF() Returns the number of days between two dates
DATE_FORMAT() Displays date/time data in different formats
7/27/2019 Ddl & Dml Command
13/38
Different use of Alter tableCREATE TABLE EMP1( ecode number(3)ename varchar2(50) );
/*********************Add column ************
Alter table emp1 add ( age number(3));
/********drop column**********
Alter table emp1 drop column age;
/*********add constraint *********
Alter table emp1 add primary key (ename);
Alter table emp1 add primary key (ecode,ename);
//sim..
Alter table emp1 add UNIQUE(ename);
//change column name (rename)
Alter table emp1 rename column ecode to e_id ;
/******************** ******************
7/27/2019 Ddl & Dml Command
14/38
/********************Modify column ******************
SQL> desc emp1;
Name Null? Type
----------------------------------------- -------- ---------------
ECODE NUMBER(3)
ENAME VARCHAR2(50)AGE NUMBER(3)
SQL> Alter table emp1 modify ( ename varchar2(100));
Table altered.
SQL> desc emp1;
Name Null? Type----------------------------------------- -------- ---------------
ECODE NUMBER(3)
ENAME VARCHAR2(100)
AGE NUMBER(3)
SQL> Alter table emp1 modify ( ename int);
Table altered.
SQL> desc emp1;
Name Null? Type
----------------------------------------- -------- ----------------------------
ECODE NUMBER(3)
ENAME NUMBER(38)AGE NUMBER(3)
7/27/2019 Ddl & Dml Command
15/38
Drop column
emp_id emp_name e_post e_salary e_age----------- ---------- ---------- ----------- --------- ---------
111 shraddha manager 150000 33
222 sagar clerk 5000 24
333 sarvesh security 5000 33
444 raj cashier 50000 32
/* drop cloumn */
alter table employee drop column e_age
7/27/2019 Ddl & Dml Command
16/38
/* output of above query
emp_id emp_name e_post e_salary
----------- ---------- ---------- -----------
111 shraddha manager 150000222 sagar clerk 5000
333 sarvesh security 5000
444 raj cashier 50000
delete from employee where emp_name='sagar;select * from employee
/* output of above query
emp_id emp_name e_post e_salary
----------- ---------- ---------- -----------
111 shraddha manager 150000
333 sarvesh security 5000
444 raj cashier 50000
7/27/2019 Ddl & Dml Command
17/38
Diff. clause (Logical,relational.)create table employee(emp_id int primary key,emp_name varchar(10),e_post
varchar(10),e_salary int);
emp_id emp_name e_post e_salary
----------- ---------- ---------- -----------111 shraddha manager 150000
222 sagar clerk 5000
333 sarvesh security 5000
444 raj cashier 50000
/* and clause */
select emp_name,e_salary from employee where e_salary>500 ande_salary
7/27/2019 Ddl & Dml Command
18/38
/* as clause */
select e_salary as e_s1 from employee
/* output of above query
e_s1-----------
150000
5000
5000
50000
7/27/2019 Ddl & Dml Command
19/38
/* distinct (eliminate duplicates) */
select distinct e_salary from employee
/* output of above query
e_salary
-----------
5000
50000
150000
/* St i ti */
7/27/2019 Ddl & Dml Command
20/38
/* String operations */create table employee(emp_id int primary key,emp_name
varchar(10),e_post varchar(10),e_salary int);
EMP_ID EMP_NAME E_POST E_SALARY---------- ---------- ---------- ----------
111 shraddha manager 150000
222 sagar clerk 5000
333 sarvesh security 5000
444 raj cashier 50000
SQL> select * from employee where emp_name like 'sa%;
EMP_ID EMP_NAME E_POST E_SALARY---------- ---------- ---------- ----------
222 sagar clerk 5000
333 sarvesh security 5000
7/27/2019 Ddl & Dml Command
21/38
select * from employee where emp_name like '%sa%
EMP_ID EMP_NAME E_POST E_SALARY
---------- ---------- ---------- ----------
222 sagar clerk 5000
333 sarvesh security 5000
select * from employee where emp_name like _ _ _
EMP_ID EMP_NAME E_POST E_SALARY
---------- ---------- ---------- ----------
444 raj cashier 50000
select * from employee where emp_name like '_ _ _ _'
no rows selected
//
select * from employee where emp_name like '_ _ _%'
EMP_ID EMP_NAME E_POST E_SALARY
---------- ---------- ---------- ----------
111 shraddha manager 150000
222 sagar clerk 5000
333 sarvesh security 5000
444 raj cashier 50000
select * from employee where emp name like ' a%'
7/27/2019 Ddl & Dml Command
22/38
select * from employee where emp_name like _a%
EMP_ID EMP_NAME E_POST E_SALARY
------ ---------- ---------- ----------
222 sagar clerk 5000
333 sarvesh security 5000
444 raj cashier 50000
1* select * from employee where emp_name like '_ _ _ _ _a%'
SQL> /
no rows selected
1* select * from employee where emp_name like '_ _ _a_%'
SQL> /
EMP_ID EMP_NAME E_POST E_SALARY
---------- ---------- ---------- ----------
111 shraddha manager 150000
222 sagar clerk 5000
1* select * from employee where emp_name like '_ _ _ _a_%'SQL> /
no rows selected
7/27/2019 Ddl & Dml Command
23/38
Not like
select * from employee where emp_name not
like '%s% ;/* output of above query
emp_id emp_name e_post e_salary----------- ---------- ---------- -----------
444 raj cashier 50000
7/27/2019 Ddl & Dml Command
24/38
/* aggregation function */emp_id emp_name e_post e_salary e_age
----------- --------- ----------- ----------- ---------
111 shraddha manager 150000 33
222 sagar clerk 5000 24
333 sarvesh security 5000 33
444 raj cashier 50000 32
/* aggregation function sum */
select sum(e_salary) from employe
/*output of above query-----------
210000
/* aggregation function avg */
select avg(e_Salary) from employe
/*output of above query
-----------52500
/* aggregation function max,min */
select max(e_Salary),min(e_salary) from employe
/*output of above query
----------- -----------
8640 40
/* f */
7/27/2019 Ddl & Dml Command
25/38
/* aggregation function count */
//it count all rowin table
select count(*)from employe-----------
4
//it count all rows in table that have non nullvalue for column name
select count(e_salary)from employe /* rows
table have not null value */-----------
4
7/27/2019 Ddl & Dml Command
26/38
Employee table
EMPNO ENAME JOB MGR SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ----------
7369 SMITH CLERK 7902 800 20
7499 ALLEN SALESMAN 7698 1600 300 30
7521 WARD SALESMAN 7698 1250 500 307566 JONES MANAGER 7839 297 1 20
14 rows selected.
7/27/2019 Ddl & Dml Command
27/38
Write down the SQL (SELECT..from..where) queries based on emp & dept
table
a) Write a query to display ename and salary of employees whose salaryis
greater than or equal to 2200.
b) Write a query to display employee name and salary of those employees
who do not have their salary in range of 2500 to 4000.
c) Write a query to display details of employee who are not getting
commission from table emp.
d) Write a query to display name, job, tittle and salary of employee who donot have manager.
e) Write a query to display name of employee whose name contain A as third
alphabet.
f) Write a query to display name of employee whose name contain T as last
alphabet.g) Write a query to display name of employee whose name contain M as
first alphabet and L as third alphabet.
7/27/2019 Ddl & Dml Command
28/38
h) List employee name and their department number for employee number
between 4000 and 5000.
i) List the minimum salary and maximum salary of each job type.j) Show the average salary for all departments with more then three people
for a job.
k) Display only the jobs with minimum salary greater than or equal to 3000.
l) Find out the number of employees having manager as job.
m) Find the average salary for each job type, remember that salesman earnscommission.
n) Show the average salary for all departments for more then three people for
a job.
o) List the count of employee grouped by department number.
p) List the sum of employees, salaries grouped by department.
q) List the maximum salary of employee grouped by their department
number.
7/27/2019 Ddl & Dml Command
29/38
a) Write a query to display ename and
salary of employees whose salary is greater
than or equal to 2200.
7/27/2019 Ddl & Dml Command
30/38
Query :
SQL> select ename,sal from emp where sal>=2200;
ENAME SAL
---------- ----------
JONES 2975
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000FORD 3000
7/27/2019 Ddl & Dml Command
31/38
b) Write a query to display employee name
and salary of those employees who do not
have their salary in range of 2500 to 4000.
7/27/2019 Ddl & Dml Command
32/38
Query:
SQL> select ename,sal from emp where sal not between 2500 and 4000;
ENAME SAL
---------- ----------
SMITH 800
ALLEN 1600
WARD 1250
MARTIN 1250
CLARK 2450
KING 5000
TURNER 1500
ADAMS 1100
JAMES 950
MILLER 1300
7/27/2019 Ddl & Dml Command
33/38
c) Write a query to display details of
employee who are not getting commission
from table emp.
7/27/2019 Ddl & Dml Command
34/38
Query:
SQL> select * from emp where comm is NULL;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
7566 JONES MANAGER 7839 02-APR-81 2975
20
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7/27/2019 Ddl & Dml Command
35/38
d) Write a query to display name, job, tittle
and salary of employee who do not have
manager.
7/27/2019 Ddl & Dml Command
36/38
Query:
SQL> select empno,ename,job,sal from emp where mgr is null;
EMPNO ENAME JOB SAL
---------- ---------- --------- ----------
7839 KING PRESIDENT 5000
7/27/2019 Ddl & Dml Command
37/38
e) Write a query to display name of employee whose name contain A as third
alphabet.
Query:
SQL> select ename from emp where ename like '__A%';
ENAME
----------
BLAKE
CLARK
ADAMS
7/27/2019 Ddl & Dml Command
38/38
f) Write a query to display name of employee whose name contain T as last alphabet.
Query:
SQL> select ename from emp where ename like '%T';
ENAME
----------
SCOTT
g) Write a query to display name of employee whose name contain M as first
alphabet and L as third alphabet.
Query:
SQL> select ename from emp where ename like 'M_L%';
ENAME
----------
MILLER