Beruflich Dokumente
Kultur Dokumente
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
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
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 product values('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
Description
DATE TIME
A date. Format: DD-MM-YYYY Note: The supported range is from '1000-01-01' to '9999-12-31' *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-1231 23:59:59' *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:SS Note: The supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC A time. Format: HH:MM:SS Note: The supported range is from '-838:59:59' to '838:59:59'
DATETIME()
TIMESTAMP()
TIME()
*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.
DATE
SQL> 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
timestamp
SQL> create table employee4(e_name varchar(20),s_date TIMESTAMP(7)) SQL> insert into employee4 values('deep','17-jun87 02.22.22') 1 row created. E_NAME S_DATE ---------- ------------Deep 17-JUN-87 02.22.22.0000000 AM
SQL> create table employee5(e_name varchar(20),s_date TIMESTAMP(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
/********************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)
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
/* output of above query emp_id emp_name e_post e_salary ----------- ------------------- ----------111 shraddha manager 150000 222 sagar clerk 5000 333 sarvesh security 5000 444 raj cashier 50000
/* as clause */ select e_salary as e_s1 from employee /* output of above query e_s1 ----------150000 5000 5000 50000
*/
/* 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
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
Not like
select * from employee where emp_name not like '%s% ; /* output of above query
emp_id emp_name e_post e_salary
/* aggregation function */
emp_id emp_name ------------------111 shraddha 222 sagar 333 sarvesh 444 raj e_post e_salary e_age ----------- ----------- --------manager 150000 33 clerk 5000 24 security 5000 33 cashier 50000 32
/* 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
Employee table
EMPNO ENAME JOB MGR SAL COMM DEPTNO ---------20 30 30 20 ---------- ---------- --------7369 SMITH CLERK 7499 7521 7566 ALLEN SALESMAN WARD SALESMAN JONES MANAGER ---------- --------- ---------7902 800 7698 7698 7839 1600 1250 297 1 300 500
14 rows selected.
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 do not 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.
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 earns commission. 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.
a) Write a query to display ename and salary of employees whose salary is 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.
Query: SQL> select ename,sal from emp where sal not between 2500 and 4000; ENAME ---------SMITH ALLEN WARD MARTIN CLARK KING TURNER ADAMS JAMES MILLER SAL ---------800 1600 1250 1250 2450 5000 1500 1100 950 1300
c) Write a query to display details of employee who are not getting commission from table emp.
Query: SQL> select * from emp where comm is NULL; EMPNO ENAME JOB MGR HIREDATE SAL ---------- ---------- --------- ---------- --------- ---------- ---------DEPTNO ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7566 JONES 20 7698 BLAKE 30 MANAGER 7839 02-APR-81 2975 COMM
MANAGER
7839 01-MAY-81
2850
d) Write a query to display name, job, tittle and salary of employee who do not have manager.
Query: SQL> select empno,ename,job,sal from emp where mgr is null; EMPNO ENAME JOB SAL ---------- ---------- -----------------7839 KING PRESIDENT 5000
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
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