order by What are 1, 2 in alignment? - oracle

select empno , deptno , sal from emp
order by 1,2
What are 1, 2 in alignment?
What is one and two? Why don't you write the column name?

The query language defines this as a shortcut.
select empno , deptno , sal from emp
order by 1,2
select empno , deptno , sal from emp
order by empno, deptno
mean precisely the same thing. The numbers refer to the column numbers in your SELECT, counting from 1.
The shortcut comes in handy if you have stuff like
select CONCAT(surname, ', ', givenname) name, empno , deptno , sal from emp
order by 1
because it saves typing. In standard SQL you'd have to write that query
select CONCAT(surname, ', ', givenname) name, empno , deptno , sal from emp
order by CONCAT(surname, ', ', givenname)
Use whichever one you please. But be careful; it makes your ORDER BY clause dependent on the order of items in your SELECT clause. The next person to work on your code may not be expecting such a dependency. Especially if that next person is you.

It works just the same, whether you use
order by empno, deptno
or
order by 1, 2
Note that - although positional sorting requires less typing, you have to synchronize such an order by clause with every select column list rearrangement. I never use it (except for quick & dirty testing queries).

Related

Unable to display the employees whose salary is grater than 1300

I'm using Oracle DB. The question is
Select all employee who jobs are either CLERK or SALESMAN and have salary greater than 1300..
Hence my answer is
select ENAME from EMP where JOB='CLERK' OR JOB='SALESMAN' AND SAL>1300;
The problem here is even though I have mentioned as salary to be greater than 1300, it is listing employee names with salary 800/1100 etc. What is wrong with my query?
The documentation list condition precedence form high to low. That shows that AND is evaluated before OR. With no parentheses your query:
select ENAME from EMP where JOB='CLERK' OR JOB='SALESMAN' AND SAL>1300;
is evaluated as
select ENAME from EMP where JOB='CLERK' OR (JOB='SALESMAN' AND SAL>1300);
so the 1300 limit is only applied to salesmen, and you will see clerks with any salary. To change that default implied precedence you need to supply parentheses, as #user7294900 showed:
select ENAME from EMP where (JOB='CLERK' OR JOB='SALESMAN') AND SAL>1300;
You could also use an IN() condition here:
select ENAME from EMP where JOB IN ('CLERK', 'SALESMAN') AND SAL>1300;
Add parentheses between AND conditions
select ENAME from EMP where (JOB='CLERK' OR JOB='SALESMAN') AND SAL>1300;

Hide column from Query

I don't want rank column but still, want the data in the same format by applying dense rank.
select ename,position,deptno,dense_rank() over(partition by deptno order by ename asc) as rank from emp where deptno in ('10','30');
Why do you need the rank just order by deptno first then ename.
SELECT ename,position,deptno,
FROM emp
WHERE deptno in ('10','30')
ORDER BY DeptNo, Ename
Using the analytical function two options derived table or CTE
Derived table/inline view.
SELECT ename,position,deptno
FROM (select ename,position,deptno,dense_rank() over(partition by deptno order by ename asc) as rank
from emp
where deptno in ('10','30')) Z
ORDER BY deptNo, rank
Common Table Expression (CTE):
with Z AS (SELECT ename,position,deptno
, dense_rank() over(partition by deptno order by ename asc) as rank
FROM emp
WHERE deptno in ('10','30'))
SELECT ename,position,deptno
FROM z
ORDER BY deptno, rank
Both these last 2 techniques simply avoid exposing the rank function to the outer query in which the results are returned. They are "Tricks" and sub-optimal execution time. unless there's a specific reason to have the rank data; I'd not use it.

Trouble With Basic Query Searching

Not sure if the this is the right place to ask but i have a question with beginner sql.
I have the table dept and emp which include:
SQL> desc dept;
Name Null? Type
----------------------------------------- -------- ----------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14)
LOC NOT NULL VARCHAR2(13)
SQL> desc emp;
Name Null? Type
----------------------------------------- -------- ----------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
I need to search for all jobs that are in department 30 and to Include the location of department 30.
What im trying is this:
SQL> select emp.job, dept.deptno, dept.loc
2 from emp, dept
3 where emp.deptno = dept.deptno
4 where deptno = '30';
where deptno = '30'
*
ERROR at line 4:
ORA-00933: SQL command not properly ended
But as you can see its not working and i have tried different variations but still no luck. Am i on the right track? How would I solve this?
It sounds like you want something like this. When you have multiple conditions in the where clause, you only specify where once and combine them with and or or conditions.
select emp.job, dept.deptno, dept.loc
from emp, dept
where emp.deptno = dept.deptno
and dept.deptno = 30;
Unless there is some reason that you really need to use the old join syntax, you probably ought to start with the SQL 99 syntax. It makes it much easier to move between databases, it makes your queries easier to read by separating join and filter conditions, and it makes life much easier when you start working on outer joins.
select emp.job, dept.deptno, dept.loc
from emp
join dept
on( emp.deptno = dept.deptno )
where dept.deptno = 30;

How can i select just one row in Oracle by row id?

I have used mysql database in my application, but I want to migrate to Oracle.
The problem is in that query:
select * from users limit ?,1;"
That query returns every row one by one depending on ?.
How can i do that in oracle?
On Oracle 12c, you could use the row limiting feature using FETCH FIRST clause.
SQL> SELECT empno, sal, deptno FROM emp ORDER BY empno DESC
2 FETCH FIRST 1 ROWS ONLY;
EMPNO SAL DEPTNO
---------- ---------- ----------
7934 1300 10
SQL>
Prior 12c solution is ROWNUM, however, if you want the row to be first sorted, then you need to do it in a sub-query -
SQL> SELECT empno, sal, deptno FROM
2 ( SELECT * FROM emp ORDER BY empno DESC
3 ) WHERE ROWNUM = 1;
EMPNO SAL DEPTNO
---------- ---------- ----------
7934 1300 10
SQL>
If the order doesn't matter to you, if you just want any random row, simply use ROWNUM.
Depending on your requirement, you could also use ANALYTIC functions such as ROW_NUMBER, RANK, DENSE_RANK.
select * from (select rownum r, u.* from users u ) where r=1;
or if you want it sorted (replace x by columnnumber or columnname):
select * from (select rownum r, u.* from users u order by x) where r=1;

get all columns after finding sum of Sal from a table

i have three columns employee_id,e_name,Sal and i want the sum of Sal group by employee_id and i want all the columns like employee_id,e_name,Sal,sum as out put in oracle.
in/put= OP=
employee_id,e_name,Sal employee_id,e_name,sal,sum
select employee_id,e_name,sal, sum(Sal) over()
from YOUR_TABLE
you will have the same value of sum for each employee

Resources