Examples Convert to Sqlserver
Examples Convert to Sqlserver
fee number(5));
Example:-
Display the details of the employees Incrementing their salary two times.
Example: Display the details of the employees whose salary is less than 3000.
Display the details of Employees whose salary is less than or equal to 3000.
Display the details of Employees whose salary is Greater than 1000 AND also whose salary is less
than 2000.
Sql> SELECT *FROM emp WHERE sal > 1000 AND sal <2000;
job='MANAGER';
Display the details of Employees whose salary is Greater than 1000 OR also whose salary is less
than 2000.
Sql> SELECT *FROM emp WHERE sal> 1000 OR sal < 2000;
Explination:-whose salaries more than 1000 or less than 2000 that all emp table display.
Display the details of employees whose salary is Greater than or Equals to 3000.
Explination:-whose salary less than 3000 that salaries all are comming.
('SMITH','ALLEN','WARD');
-----------------------------------------------------------------------------------------------------
Display the employees whose name ends with ‘S’ in EMP table
----------------------------------------------------------------------------------------------------------
Display the employees whose names are having second letter as ‘L’ in EMP table
-----------------------------------------------------------------------------------------------------
Sql> select ename ,hiredate from emp where hiredate like '%JAN%';
SQL> select empno,ename,job ,hiredate from emp where hiredate like '%-FEB-81';
Sql> select *from dept where dname like '__/_%' escape '/';
Display the employees whose names are not having second letter as ‘L’ in EMP table?
Display the employees whose names are not start with ‘S’ in EMP table.?
Sql> select ename ,hiredate from emp where hiredate not like '%JAN%';
Sql> select empno,ename,job from emp where ename not like '_O%';
Display the employees whose names are second letter start with ‘R’ from ending.?
-----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------
sql>select grade as "SalGrade", hisal "High Salary Range",losal "Low Salary Range"
from salgrade;
group by deptno
having count(deptno)>3;
group by deptno
having avg(sal)>2500;
where job='MANAGER'
group by deptno
having avg(sal)<1000;
group by deptno;
group by job;
Example: Display employee records order by ‘ename’ in descending order. Here we need to
explicitly specify the keyword ‘DESC’.
ORDER BY 2;
Here 2 represents second column. So, data will be displayed with ename in ascending order.
Sql> select ename,job,sal,deptno from emp where sal>=2000
order by Annualsal;
order by deptno,job,sal;
Sql> select *from emp where job=(select job from emp where ename='SMITH')
AND ENAME<>'SMITH'
where loc='DALLAS');
where dname='SALES');
where ename='FORD')
AND
where dname='SALES'));
group by deptno);
where deptno=d.deptno
and deptno=10);
where c.empno=p.mgr);
where deptno=d.deptno
and deptno=10);
where c.empno=p.mgr);
sql>select empno,ename,job,sal,nvl(comm,0),sal+nvl(comm,0)
from emp
where dname='SALES'));
where job='SALESMAN');
1. SQL> select empno, ename from emp where deptno=(select deptno from dept where
dname='RESEARCH');
2. SQL> select empno, ename from emp where deptno in (select deptno from dept where loc in
('NEW YORK','CHICAGO'));
3. SQL> select dname from dept where deptno in ( select deptno from emp where job ='ANALYST');
4. SQL> select empno, ename, mgr from emp where mgr = (select empno from emp where
ename='JONES');
5. SQL> select empno, ename, mgr from emp where mgr = (select mgr from emp where
ename='JONES')
6. SQL> select empno, ename, job from emp where deptno in ( select deptno from dept where
dname in ('SALES','ACCOUNTING'))
7. SQL> select empno, ename, job from emp where deptno in ( select deptno from dept where
dname in ('SALES','RESEARCH')) and empno in (select mgr from emp)
8. SQL> select empno, ename from emp where empno not in ( select mgr from emp where mgr is
not null)
9. select empno, ename from emp where empno in (select mgr from emp group by mgr
10. SQL> select dname from dept where deptno in (select deptno from emp group by deptno having
count(*) >=5)
11. SQL> select deptno, job, count(*) from emp where job = 'SALESMAN' group by deptno, job
having count(*) >=3
12. SQL> select empno, ename, deptno from emp where empno in (select mgr from emp group by
mgr
having count(*) >= 2) and deptno in (select deptno from dept where dname='RESEARCH' or
dname='ACCOUNTING')
13. SQL>select max(sal) from emp where sal < (select max(sal) from emp);
14. SQL> select max(sal) from emp where sal < (select max(sal) from emp where sal < (select
max(sal) from emp where sal < (select max(sal) from emp)))
15.sql> SELECT * FROM EMP r1
7. SQL> SELECT * FROM emp WHERE sal BETWEEN 2000 and 3000;
10. SQL> SELECT * FROM emp WHERE mgr IS NULL AND COMM IS NULL;
12. SQL> SELECT * from emp WHERE job = 'MANAGER' and deptno in (10,20);
13. SQL> SELECT * FROM emp WHERE job IN ('CLERK','ANALYST') AND sal >= 1000 AND deptno IN
(20,30);
14. SQL> SELECT * FROM emp where deptno in (20,30) and comm IS NULL;
15. SQL> SELECT * FROM emp WHERE ename LIKE ('A%') OR ename LIKE ('S%');
16. SQL>SELECT * FROM emp WHERE ename NOT LIKE ('%S') AND deptno IN (20,30);
17. SQL> SELECT * FROM emp where comm IS NOT NULL AND sal > 1500 AND deptno = 30;
18. SQL> SELECT * FROM emp where comm IS NOT NULL AND sal > 1500 AND job = 'MANAGER'
19. SQL> SELECT * FROM emp WHERE job = 'MANAGER' OR job = 'CLERK' AND sal >=2000 AND
deptno NOT IN (10,20);
21. SQL> SELECT * from emp WHERE sal NOT BETWEEN 2000 AND 3000 AND job LIKE ('%MAN
%');
SQL> select *from emp where deptno in(select deptno from dept
where dname='SALES');
where job='CLERK');
4) display the employee who are working are working in “new work”
where dname='SALES'
group by deptno);
Department
where dname='ACCOUNTING');
1)list out the employees who earn more than every employee in department 30
Where deptno=30);
2) list out the employees who earn more than the lowest salary in deptno 30
where deptno=30);
3)find out which department does not have any employees
where e.deptno=d.deptno);
4)find out employees who earn greater than the average salary for their department
5)List the empno, ename, sal, dname of all the ‘Mgrs’ and
JOB IN('MANAGER','ANALYST')AND
MONTHS_BETWEEN(SYSDATE,HIREDATE)/12 > 7
where e.deptno=d.deptno(+)
and e.deptno(+)=10
order by e.deptno;
sql> select emp.ename from emp left OUTER join dept on (emp.deptno =
on e.deptno=d.deptno
where loc='CHICAGO';
* If we want to see the indexName that time we writy the below example
Example:-
sql> select index_name from user_constraints
where table_name='EMP';
Enabling constraints:-
• The alter table statement with the enable clause is used for the purpose.
Syntax:-
<constraintname>;
contact_name varchar2(50),
);
contact_name varchar2(50),
CONSTRAINT supplier_unique UNIQUE (supplier_id, supplier_name)
);
sql>CREATE TABLE t1 (
c1 NUMBER,
c2 VARCHAR2(30),
c3 VARCHAR2(30),
Sql>CREATE TABLE t1 (
c1 NUMBER,
c2 VARCHAR2(30),
c3 VARCHAR2(30),
check(job in('ANALYST','CLERK','MANAGER','PRESIDENT','SALESMAN')),
check(sal+comm<=100000));
• union
• union
union all
intersect
except
'MANAGER');
except
select rownum,ename from emp where rownum<6;
as
as
sql> create view payinfo as select empno Ecode,sal Basic,sal*0.25 Da,sal*0.35 HRA,
order by Annual;
B.sal,sum(A.sal)cum_sal
as
where deptno=20;
refresh on commit
as
group by job
as
where e.deptno=d.deptno
group by dname,d.deptno;
group by rollup(job,deptno);
group by cube(deptno,job);
group by cube(deptno,job);
end
from emp;
from emp;
group by deptno,rollup(deptno,job)
having group_id()=0;
where r<=5