Kurs Oracle SQL online. Sortowanie rekordów

W tej lekcji kursu Oracle SQL zaczniemy sortować rekordy w wyniku działania zapytania.

Jeśli chcesz posortować rekordy należy użyć klauzuli ORDER BY. Zatem do dzieła 🙂

Przykład1. Wyświetl pracowników sortując wynik po nazwiskach i imionach rosnąco.

SELECT *
FROM employees
ORDER BY last_name, first_name;

Dla rekordów z takimi samymi wartościami w kolumnie last_name, sortowanie nastąpi w oparciu o zawartość kolumny first_name. W naszym przypadku jest tak dla pracowników o nazwisku Grant – ponieważ były te same wartości w kolumnie nazwiska sortowanie tych rekordów nastąpiło z wykorzystaniem kolumny imię.

Sortowanie może być:

  • Rosnące: ORDER BY kolumna =  ORDER BY kolumna ASC
  • Malejące: ORDER BY kolumna DESC

Przykład 2. Wyświetl pracowników sortując wynik po nazwiskach malejąco, a dla tych samych nazwisk po imionach rosnąco.

SELECT *
FROM employees
ORDER BY last_name DESC, first_name;

Kolumny do sortowania można wskazywać używając:

  • Nazwy kolumny
  • Aliasu kolumny
  • Numeru kolumny w zapytaniu

SELECT first_name imie,
last_name nazwisko,
salary pensja
FROM employees;

Przykład 3. Zmodyfikuj powyższe zapytanie tak, aby wynik posortować po nazwiskach rosnąco, a dla tych samych nazwisk po imionach malejąco – korzystając z nazw kolumn

SELECT first_name imie,
       last_name nazwisko,
       salary pensja
FROM employees
ORDER BY last_name, first_name DESC;

Przykład 4. Zmodyfikuj powyższe zapytanie tak, aby wynik posortować po nazwiskach rosnąco, a dla tych samych nazwisk po imionach malejąco – korzystając z aliasów kolumn

SELECT first_name imie,
       last_name nazwisko,
       salary pensja
FROM employees
ORDER BY nazwisko, imie DESC;

Przykład 5. Zmodyfikuj powyższe zapytanie tak, aby wynik posortować po nazwiskach rosnąco, a dla tych samych nazwisk po imionach malejąco – korzystając z numerów kolumn w zapytaniu

SELECT first_name imie,
       last_name nazwisko,
       salary pensja
FROM employees
ORDER BY 2, 1 DESC;

W kolejnej lekcji poznasz najważniejsze wbudowane funkcje. Czyli m.in. jak wybierać inicjały z imion i nazwisk, jak zaokrąglać kwoty. Zapraszam!

 

 

 

This entry was posted in Oracle SQL, Oracle SQL Podstawy, SQL and tagged , , , , . Bookmark the permalink.

3 Responses to Kurs Oracle SQL online. Sortowanie rekordów

  1. adderek says:

    Ja bym jeszcze dodał, że operacja sortowania nie zwróci nam żadnych wyników dopóki Oracle nie posortuje wszystkich wierszy.
    Jest to ważne np. w serwisach WWW które mają zaprezentować wynik zapytania jak najszybciej (np. listę produktów).

    Przykład:
    Wyobraźmy sobie wielki magazyn z tysiącami paczek.
    Mogę komuś zlecić podanie mi pierwszej z brzegu paczki – dostanę ją szybko ale nie wiem z jaką zawartością.
    Mogę zlecić wyszukanie konkretnej paczki – co potrwa zapewne dłużej.
    A co gdybym polecił posortowanie paczke względem rozmiarów i podanie 10 najmniejszych? Tak właśnie działa ORDER BY.

    I jeszcze słówko wstępu do zaawansowanych tematów Oracle:
    (1) Pół biedy jeśli ktoś już paczki zmierzył i przegląda segregator. (2) Jeśli ktoś na dolnych półkach ułoży duże paczki a na górnych małe to też troszkę zyskamy (nie trzeba przeglądać wszystkich paczek). (3) Gorzej gdy każdą paczkę trzeba z osobna dopiero zmierzyć.
    Opcja (1) to indexy. Opcja (2) to partycjonowanie. Opcja (3) to full table scan.

  2. darek says:

    Przydałoby się jeszcze pokazać sortowanie według wyniku działania funkcji, np.;
    SELECT last_name, phone_number
    FROM employees
    ORDER BY SUBSTR(phone_number, 5, 3);

    a może nawet sortowanie według wartości kolumny z innej tabeli 🙂

    • Rafał Stryjek says:

      Sortowanie po wyniku funkcji to ważne tematy.. Zostawiam je na kurs dla zaawansowanych.. tak jak i NULLS FIRST .. 🙂

  3. Andrewpag says:
    Your comment is awaiting moderation.

    Appreciate it. Loads of knowledge!

    increase libido female viagra does viagra prevent ejaculation
    buy viagra online
    generic propecia viagra – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  4. Andrewpag says:
    Your comment is awaiting moderation.

    You actually explained it wonderfully.

    free sites computer viagra find blog viagra soft tabs
    cheap viagra
    streets of rage viagra – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  5. Andrewpag says:
    Your comment is awaiting moderation.

    You actually said this well.

    zoloft viagra ejaculation viagra without rx
    viagra pills
    generic viagra sale – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  6. Andrewpag says:
    Your comment is awaiting moderation.

    You stated this wonderfully!

    vicoden morphine viagra viagra private instruction
    generic viagra
    real viagra online real pfizer – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  7. Andrewpag says:
    Your comment is awaiting moderation.

    You mentioned it very well!

    viagra rules viagra viagra buy search find free
    viagra pills
    viagra vs levitra strength – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  8. Andrewpag says:
    Your comment is awaiting moderation.

    Amazing a good deal of superb knowledge.

    viagra como se toma viagra free sites computer search buy
    generic viagra 100mg
    purchasing viagra in bali – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  9. Andrewpag says:
    Your comment is awaiting moderation.

    You said it perfectly!

    audio viagra torrent viagra exp date
    generic viagra
    delivery overnight viagra – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  10. Andrewpag says:
    Your comment is awaiting moderation.

    You actually expressed that effectively!

    generic viagra without prescription paypal best prices on canadian viagra
    generic viagra 100mg
    carvedilol and viagra – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  11. Andrewpag says:
    Your comment is awaiting moderation.

    Thanks a lot. I appreciate it!

    blog viagra how to boost your viagra
    buy generic viagra
    free viagra canada – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  12. Andrewpag says:
    Your comment is awaiting moderation.
  13. Andrewpag says:
    Your comment is awaiting moderation.

    Thanks! Plenty of facts.

    viagra and valuim kamagra kamagra kamagra lovegra uk viagra
    viagra without a doctors prescription
    10mg vs 20mg viagra experiance – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  14. Andrewpag says:
    Your comment is awaiting moderation.

    You reported this adequately!

    wholesale 60 c viagra masturbaton and viagra
    viagra generic
    vegtable viagra – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  15. Andrewpag says:
    Your comment is awaiting moderation.

    Terrific content. Appreciate it.

    toronto viagra viagra levitra valium cialis tramadol hydrocodone
    viagra for women
    viagra in young men – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  16. Andrewpag says:
    Your comment is awaiting moderation.

    Reliable advice. Appreciate it.

    lyrics abracadabra im up like viagra phone call viagra vendor
    viagra online
    viagra online in canada – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  17. Andrewpag says:
    Your comment is awaiting moderation.

    You actually reported that wonderfully!

    who owns viagra locked in syndrome and viagra
    viagra for women
    viagra photophobia – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  18. Andrewpag says:
    Your comment is awaiting moderation.

    You definitely made the point!

    least exspencive viagra cialis viagra levitra pump
    viagra for sale uk
    enhancing the viagra experience – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  19. Andrewpag says:
    Your comment is awaiting moderation.

    Incredible tons of fantastic facts.

    female viagra tablets cheap viagra stories real people’s experiences pharmacy
    generic viagra
    viagra home – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  20. Andrewpag says:
    Your comment is awaiting moderation.

    Awesome tips. With thanks!

    viagra online toronto canada husband takes viagra
    buy generic viagra
    viagra illustrations – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  21. Andrewpag says:
    Your comment is awaiting moderation.

    Fantastic information, Kudos.

    generic viagra caverta 2c cialis cheap what heart condition uses viagra
    viagra pills
    viagra experiences users – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  22. Andrewpag says:
    Your comment is awaiting moderation.
  23. Andrewpag says:
    Your comment is awaiting moderation.

    Amazing many of awesome info.

    jamaica blog negril sex viagra discount generic viagra panama
    viagra without a doctors prescription
    best natural viagra – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  24. Andrewpag says:
    Your comment is awaiting moderation.

    Kudos, Awesome information!

    viagra and nitrates in th er kamagra viagra silden
    generic viagra 100mg
    human viagra – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  25. Andrewpag says:
    Your comment is awaiting moderation.

    Valuable content. Thank you!

    healthy man viagra viagra no longer works
    buy generic viagra
    vipps approved viagra – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  26. Andrewpag says:
    Your comment is awaiting moderation.

    With thanks! A good amount of info.

    buy viagra onlines water soluble viagra
    viagra for sale uk
    viagra lotion – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  27. Andrewpag says:
    Your comment is awaiting moderation.

    You’ve made your stand quite effectively!.

    does viagra work after eating viagra cartooms
    viagra for sale uk
    viagra for diabetics – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  28. Andrewpag says:
    Your comment is awaiting moderation.

    Fine info. Appreciate it.

    i used viagra buy com online phentermine viagra
    generic viagra 100mg
    cheap soft viagra – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  29. Andrewpag says:
    Your comment is awaiting moderation.

    You actually mentioned it really well!

    lung diseases treated with viagra viagra danger dogs
    buy generic viagra
    awesome viagra bull – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  30. Andrewpag says:
    Your comment is awaiting moderation.

    Wow a good deal of wonderful info.

    problems with viagra find sites computer shop viagra edinburgh
    viagra without a doctors prescription
    age recommend for taking viagra – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  31. Andrewpag says:
    Your comment is awaiting moderation.

    Great write ups. Thanks!

    husband takes viagra viagra falls play
    viagra generic
    do pornstars use viagra – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  32. Andrewpag says:
    Your comment is awaiting moderation.

    Nicely put. Regards!

    viagra generico impotencia viagra side effects swelling prostate pain
    viagra for women
    wiki viagra – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  33. Andrewpag says:
    Your comment is awaiting moderation.

    Thank you, Loads of info!

    viagra online insurance net doctor group viagra
    buy generic viagra
    viagra c best supplier – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  34. Andrewpag says:
    Your comment is awaiting moderation.

    Good facts. Cheers!

    compare cialis vs viagra is so called generic viagra safe
    buy viagra online
    cialis or viagra – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  35. Andrewpag says:
    Your comment is awaiting moderation.

    You said that perfectly.

    viagra gold what to know about viagra jelly
    buy viagra
    hugh hefner’s viagra supply – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  36. Andrewpag says:
    Your comment is awaiting moderation.

    You said it nicely.!

    prescribing viagra blood high pressure viagra
    generic viagra 100mg
    pumpkin pie vs viagra – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  37. Andrewpag says:
    Your comment is awaiting moderation.
  38. Andrewpag says:
    Your comment is awaiting moderation.

    Nicely put. Regards!

    how do you take viagra teenager taking viagra
    generic viagra online
    coupons for cialis viagra levitra – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  39. Andrewpag says:
    Your comment is awaiting moderation.

    Seriously many of awesome data!

    viagra lawsuit settlement silagra penegra brand name viagra cumwithuscom
    viagra online
    viagra online overnight delivery – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  40. Andrewpag says:
    Your comment is awaiting moderation.

    Kudos! Quite a lot of data.

    generia viagra can viagra help psychological ed
    viagra online
    viagra experience – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  41. Andrewpag says:
    Your comment is awaiting moderation.

    Superb material. Many thanks!

    severe depression viagra doesnt work viagra chemist nottingham
    viagra 100mg
    find sites computer shop viagra href – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  42. Andrewpag says:
    Your comment is awaiting moderation.

    Whoa all kinds of excellent advice!

    cheap u s viagra cilias vs viagra
    viagra online
    compare cialis vs viagra – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  43. Andrewpag says:
    Your comment is awaiting moderation.

    Amazing content. Thank you.

    find viagra online viagra hearing loss
    generic viagra 100mg
    viagra lisinopril interaction – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  44. Andrewpag says:
    Your comment is awaiting moderation.

    You made your position very nicely!.

    subaction showcomments viagra smyle older price for generic viagra
    generic viagra 100mg
    viagra gel where to buy – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  45. Andrewpag says:
    Your comment is awaiting moderation.

    Info clearly utilized!.

    problem viagra vision does viagra help with jet lag
    generic viagra 100mg
    lisinopril and viagra interaction – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  46. Andrewpag says:
    Your comment is awaiting moderation.

    You actually suggested this superbly.

    xtabentun mexican viagra viagra and ibuprofen
    viagra without a doctor prescription
    viagra sealis – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  47. Andrewpag says:
    Your comment is awaiting moderation.

    Nicely put, Thank you.

    viagra and woman women take viagra
    generic viagra
    viagra effects on healthy males – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  48. Andrewpag says:
    Your comment is awaiting moderation.

    Kudos. Quite a lot of postings.

    female viagra type drug viagra gelly
    generic viagra online
    online viagra store – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  49. Andrewpag says:
    Your comment is awaiting moderation.

    You said this exceptionally well.

    cakes shaped like viagra viagra empty stomache
    buy viagra online
    viagra to buy – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  50. Andrewpag says:
    Your comment is awaiting moderation.

    Nicely put. Cheers!

    is my lover taking viagra viagra in teens for hypertension
    generic viagra
    viagra drug – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  51. Andrewpag says:
    Your comment is awaiting moderation.

    Thank you! I like this.

    panax ginseng vs viagra viagra or cealis
    buy viagra online
    2buy levitra online viagra – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  52. Andrewpag says:
    Your comment is awaiting moderation.
  53. Andrewpag says:
    Your comment is awaiting moderation.
  54. Andrewpag says:
    Your comment is awaiting moderation.

    Great content. Kudos.

    viagra extacy ashanti viagra cialis canadian pharmacy
    cheap viagra
    generic viagra silagra cumwithuscom – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  55. Andrewpag says:
    Your comment is awaiting moderation.

    Very good data. Many thanks.

    viagra buy 1 cialis viagra levitra pump
    viagra pills
    the 7dwarfs using viagra – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  56. Andrewpag says:
    Your comment is awaiting moderation.

    With thanks. Terrific stuff.

    non generic viagra viagra contraindications
    viagra without a doctors prescription
    viagra pointer cursor – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  57. Andrewpag says:
    Your comment is awaiting moderation.

    Terrific tips. With thanks!

    viagra overnight best price viagra store
    viagra pills
    drugs giant viagra pill cialis men – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  58. Andrewpag says:
    Your comment is awaiting moderation.

    You made your point extremely nicely!.

    best price generic viagra online prescription dithiazide and viagra
    viagra online
    alternative viagra zenegra – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  59. Andrewpag says:
    Your comment is awaiting moderation.

    This is nicely expressed! !

    viagra delayed ejaculation viagra exctasy
    cheap viagra
    shop and compare charles linskaill viagra – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  60. Andrewpag says:
    Your comment is awaiting moderation.

    You actually mentioned it really well.

    viagra find sites computer search edinburgh penis erection problem viagra cialys
    viagra without a doctors prescription
    genetic viagra europe – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  61. Andrewpag says:
    Your comment is awaiting moderation.

    Wonderful data. Many thanks!

    taking viagra and alcohol cheap gerneric viagra
    generic viagra 100mg
    review viagra vs cialis – https://genericviagramsl.com

    Davidurgef
    Terenceidoge
    VictorRalia

  62. Andrewpag says:
    Your comment is awaiting moderation.

    With thanks! I enjoy this!

    viagra generic india pharmacy sildenafil citrate united states viagra interaction flomax
    viagra generic
    210 area code viagra – https://genericviagrarcp.com

    Davidurgef
    Terenceidoge
    VictorRalia

  63. Andrewpag says:
    Your comment is awaiting moderation.

    Really plenty of amazing advice!

    can you take viagra with achohol phentermine viagra
    buy viagra online
    teen boys taking viagra – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  64. Andrewpag says:
    Your comment is awaiting moderation.

    Wonderful forum posts. Many thanks.

    multiple erections viagra tesco stores viagra
    generic viagra 100mg
    edinburgh find viagra search sites – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  65. Andrewpag says:
    Your comment is awaiting moderation.

    This is nicely said. !

    from generic india viagra diabetes viagra
    viagra for sale uk
    natural over the counter cialas viagra – https://genericviagrantx.com

    Davidurgef
    Terenceidoge
    VictorRalia

  66. Andrewpag says:
    Your comment is awaiting moderation.

Leave a Reply

Your email address will not be published.