Display Alternate Records | Ep-3 | Top 20 SQL Interview Questions | GeeksforGeeks

Поділитися
Вставка
  • Опубліковано 29 лис 2024

КОМЕНТАРІ • 49

  • @balajisundar9867
    @balajisundar9867 9 місяців тому +12

    Select * from (Select *,
    ROW_NUMBER() OVER(order by salary) as rn from Employee)
    where MOD (rn, 2) =0
    the above query will display all the even number, replace 1 with 0 to get all the odd numbers!

  • @sukhpalsingh4744
    @sukhpalsingh4744 3 роки тому +9

    bro rownum can be used with asterisk to fetch the whole data of the table, use query- select rownum,b.* from emp b;

  • @ArunArun-di1bq
    @ArunArun-di1bq 5 місяців тому +6

    ==> for even
    select * from TABLEname
    where rol_no % 2 = 0 order by roll_no;

  • @surajkumarsaw7398
    @surajkumarsaw7398 11 місяців тому +1

    with cte_row_num as (
    select *, row_number() over(order by (select null)) as rn from table
    )
    select * from cte_row_num where rn%2=0

  • @saurabhkanauji704
    @saurabhkanauji704 2 місяці тому

    select row_number() over(order by emp_no) as row_num, emp_no, department, salary
    from
    (
    select emp_no, department, salary
    from employees
    )as sub_query
    where emp_no %2 0;

  • @krishnaawasthi2120
    @krishnaawasthi2120 3 роки тому +2

    we can use rank() with CTid to give rownum to every records for postgres DB, because in postgres rownum doesn't work :)

  • @alokawasthi1478
    @alokawasthi1478 4 роки тому +4

    why were we required to use subquery. I thought it could be done without any subquery but it didn't work. Can someone tell me the reason

    • @abhishekguptasarma2060
      @abhishekguptasarma2060 3 роки тому +1

      Oracle's ROWNUM is a pseudo column that numbers the rows in a result set. Unfortunately, it numbers the rows in the set before ordering is applied. So combining the two in the same query won't get you the results you expect. Instead, you should move your selection and ordering into a subquery, and use ROWNUM only on the outer query.

  • @akshatjain6854
    @akshatjain6854 4 роки тому +4

    whats the use of outer subquery?

    • @abhishekguptasarma2060
      @abhishekguptasarma2060 3 роки тому +2

      Oracle's ROWNUM is a pseudo column that numbers the rows in a result set. Unfortunately, it numbers the rows in the set before ordering is applied. So combining the two in the same query won't get you the results you expect. Instead, you should move your selection and ordering into a subquery, and use ROWNUM only on the outer query.

  • @neerajgupta6839
    @neerajgupta6839 4 роки тому +3

    Why there is outer subquery used like select * from

    • @AnandKumar-kz3ls
      @AnandKumar-kz3ls 3 роки тому

      rownum gets its physical memory after the where clause so you need to make another table then use mod fun

    • @abhishekguptasarma2060
      @abhishekguptasarma2060 3 роки тому

      Oracle's ROWNUM is a pseudo column that numbers the rows in a result set. Unfortunately, it numbers the rows in the set before ordering is applied. So combining the two in the same query won't get you the results you expect. Instead, you should move your selection and ordering into a subquery, and use ROWNUM only on the outer query.

  • @hardikvegad3508
    @hardikvegad3508 3 роки тому +3

    how to deal with this error:
    error 1248: every derived table must have its own alias sql

    • @illarasool4472
      @illarasool4472 3 роки тому +2

      YOU ARE MISSING AS CLAUSE USE THIS:
      SELECT * FROM(
      SELECT emp_no, salary, ROW_NUMBER() OVER(ORDER BY emp_no) AS row_num
      from salaries
      order by row_num
      ) AS k
      WHERE MOD(row_num,2) = 0

    • @hardikvegad3508
      @hardikvegad3508 3 роки тому

      @@illarasool4472 thank you 🙌🏻

    • @hardikvegad3508
      @hardikvegad3508 3 роки тому

      @@illarasool4472 vel 15, State 1, Line 39
      The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

  • @lovelyjain7839
    @lovelyjain7839 4 роки тому +2

    Can we use rownum in all database ?

  • @SahilGupta-cd8lc
    @SahilGupta-cd8lc 3 місяці тому

    Select*
    From employee
    Where MOD(id,2)=0

  • @krunalpatel2228
    @krunalpatel2228 4 роки тому +3

    how to do it in mysql

    • @rohan8758
      @rohan8758 9 місяців тому

      MySql query, sorry for late reply after learning:
      select * from
      (select row_number() over (order by salary desc) as rowNum, Emp_Id, Project, Salary
      from EmployeeSalary) as firstQuery
      where rowNum % 2 != 0;

  • @AkashGholve
    @AkashGholve Рік тому +4

    for SSMS (MS SQL) people:
    try this,
    SELECT empID, empName, empSalary
    FROM (
    SELECT empID, empName, empSalary, ROW_NUMBER() OVER (ORDER BY empsalary DESC) AS Rn
    FROM emp
    ) AS altsal
    WHERE Rn % 2 = 0;

  • @archanashirsat5528
    @archanashirsat5528 4 роки тому +16

    Instead of subquery can we write
    select rownum rn, empno, sal, ename from emp
    order by rn
    where mod(rn,2)!=0

    • @TheShy007
      @TheShy007 4 роки тому +2

      Is it required to write order by rn here as it will be in ascending order by default right?

    • @aravindravva3833
      @aravindravva3833 4 роки тому

      @@TheShy007 yup its not required

    • @pintukumar-uk5nt
      @pintukumar-uk5nt 4 роки тому +2

      No, Order by clause always in last. you can write like :
      select * from(
      select rownum rn, employee_id, SALARY from emp
      order by rn)
      where mod(rn,2)!=0;

    • @AnandKumar-kz3ls
      @AnandKumar-kz3ls 3 роки тому

      use this statement Select SELECT empno as e from emp where e='Some_name'; it will still give you an error you can't refer to field aliases in the WHERE clause.

  • @TheCodeWhisperer0o0
    @TheCodeWhisperer0o0 5 місяців тому

    bro, just put the link of the database in the comment description!!

  • @rohitmunde3170
    @rohitmunde3170 3 роки тому +1

    means not equal to bro at 2:50

  • @Gowtham745
    @Gowtham745 Рік тому

    What if the gap between records are 3 to get this how should we query like row number 1, 4, 7, 10

    • @LearnerVilla
      @LearnerVilla Рік тому +1

      you can try "where mod(rn,3) == 1;"

  • @ShubhamGupta-yr5cw
    @ShubhamGupta-yr5cw 4 роки тому +3

    What if we not use rownumber in this query?

  • @ashutoshranjan4644
    @ashutoshranjan4644 Рік тому

    If you are a mysql user then this query might be helpful to you to display alternate record
    set @row_number:=0;
    select sequential_row, id from
    (
    select @row_number:=@row_number+1 as sequential_row, id
    from employee
    order by salary
    ) as numbered_row
    where mod(sequential_row, 2) = 0;

  • @chiru6753
    @chiru6753 5 років тому +3

    Can you please send the oracle live sql link

  • @learnworld8049
    @learnworld8049 2 місяці тому

    -- select * from Employees where EmployeeID%2!=0;

  • @pranatimahananda7289
    @pranatimahananda7289 3 роки тому

    What is rn please explain
    Thanks in advance

    • @ashraf0011
      @ashraf0011 3 роки тому

      rn is alias name, if very big name we have to handle, we use alias name like any small name which can use everywhere

  • @vishalsonawane.8905
    @vishalsonawane.8905 7 місяців тому

    Done

  • @codeset7810
    @codeset7810 10 місяців тому

    with temp as
    (select *,dense_rank() over(order by id asc ,salary asc) as rankk from employee)
    select * from temp where rankk%2!=0;

  • @praveenj3112
    @praveenj3112 5 років тому

    Thanks

    • @3sh66
      @3sh66 5 років тому

      Thank u bro .Plzz do like share and subscribe

  • @aarzoo_ahuja
    @aarzoo_ahuja 4 роки тому

    How to do it in mysql?

    • @tyagiFit
      @tyagiFit 3 роки тому +1

      use "rank over() as rownum" (this will work for MySQL and SQL server)

  • @tusharbansal1464
    @tusharbansal1464 4 роки тому

    You said that we can't use rownum with * but what if we want to fetch all the columns and rownum, there must some other way to do it?