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. Larrystilt says:
    Your comment is awaiting moderation.

    Kudos. I appreciate it!

    canada’s viagra low cost alternatives and to viagra
    viagra without a doctors prescription
    best generic viagra canada wholesale – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  4. Larrystilt says:
    Your comment is awaiting moderation.

    Great postings. Thank you!

    cialis viagra cough sinus opposite reaction to viagra
    viagra pills
    clininc viagra prescriptions charlotte nc – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  5. Larrystilt says:
    Your comment is awaiting moderation.

    Nicely put. Regards.

    paroxetine viagra viagra bob is happy commercial
    viagra for women
    cheap viagra soft tabs – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  6. Larrystilt says:
    Your comment is awaiting moderation.

    This is nicely said! .

    which works betters cialis or viagra best generic viagra prices
    cheap viagra
    viagra viagra drug interaction data – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  7. Larrystilt says:
    Your comment is awaiting moderation.

    You actually explained it wonderfully!

    taking viagra before penis pumping viagra physicians in houston
    generic viagra 100mg
    how by viagra – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  8. Larrystilt says:
    Your comment is awaiting moderation.

    Amazing tips. Kudos.

    viagra ordering phone number viagra fet
    buy viagra
    generic viagra online drugstores – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  9. Larrystilt says:
    Your comment is awaiting moderation.

    Wonderful write ups, Regards.

    viagra casserole tga and viagra
    viagra online
    viagra mn teachers – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  10. Larrystilt says:
    Your comment is awaiting moderation.

    You reported that very well.

    which is better viagra levitra cialis viagra non prescription deal sale
    generic viagra 100mg
    is the generic viagra online real – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  11. Larrystilt says:
    Your comment is awaiting moderation.

    You explained this really well.

    russian viagra death cialis with viagra
    viagra for women
    viagra cancun – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  12. Larrystilt says:
    Your comment is awaiting moderation.

    With thanks. I enjoy it.

    water soluble viagra viva viagra theme song
    generic viagra online
    viagra delivered from uk pharmacy – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  13. Larrystilt says:
    Your comment is awaiting moderation.

    Thanks a lot! I enjoy it!

    viagra and mexican jumping beans viagra from us sellers
    generic viagra
    discounts for viagra cialis – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  14. Larrystilt says:
    Your comment is awaiting moderation.

    You said it adequately.!

    half price pharmacy viagra animals on viagra
    buy viagra
    viagra purchases – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  15. Larrystilt says:
    Your comment is awaiting moderation.

    This is nicely put! .

    viagra kamagra vs hugh hefner viagra
    viagra 100mg
    viagra sample pack – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  16. Larrystilt says:
    Your comment is awaiting moderation.

    This is nicely put! .

    viagra in stores expired date viagra
    viagra for sale uk
    reviews generic viagra – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  17. Larrystilt says:
    Your comment is awaiting moderation.

    Reliable info. Thank you!

    water melon rine viagra how can i make homemade viagra
    cheap viagra
    viagra juice – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  18. Larrystilt says:
    Your comment is awaiting moderation.

    You actually suggested this well!

    dosages of viagra target google viagra order cheap
    cheap viagra
    best prices on brand viagra – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  19. Larrystilt says:
    Your comment is awaiting moderation.

    Many thanks! Very good stuff.

    medical journals pulmonary hypertension viagra on line pharmacy viagra
    viagra online
    viagra and nitrites – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  20. Larrystilt says:
    Your comment is awaiting moderation.

    Fine posts. Many thanks.

    wat does viagra do viagra oysters
    generic viagra 100mg
    cartoon viagra – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  21. Larrystilt says:
    Your comment is awaiting moderation.

    Whoa many of great info!

    viagra cheap no prescription viagra shipped overnight
    buy generic viagra
    viagra fuck – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  22. Larrystilt says:
    Your comment is awaiting moderation.

    Whoa lots of valuable info.

    which viagra get free viagra
    buy viagra
    viagra experiment – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  23. Larrystilt says:
    Your comment is awaiting moderation.

    Seriously all kinds of useful data.

    viagra pills for sale uk female viagra europe
    viagra without a doctor prescription
    watermellon rind viagra – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  24. Larrystilt says:
    Your comment is awaiting moderation.

    You said it adequately..

    viagra doseage herbal viagra alternatives
    viagra for sale uk
    spiked drinks viagra – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  25. Larrystilt says:
    Your comment is awaiting moderation.

    You stated that terrifically.

    blue cross viagra holdere a href womens viagra a
    viagra 100mg
    free sites computer search viagra – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  26. Larrystilt says:
    Your comment is awaiting moderation.

    Excellent content. Thanks.

    piadas de viagra viagra on line
    buy viagra online
    car in viagra commercial – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  27. Larrystilt says:
    Your comment is awaiting moderation.

    Many thanks, Plenty of facts!

    viagra and metoprolol er succinate buy viagra online online a href
    generic viagra online
    cialis generic levitra review viagra – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  28. Larrystilt says:
    Your comment is awaiting moderation.

    Information certainly used..

    green dot viagra board novartis viagra uk
    cheap viagra
    viagra fuerteventura market – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  29. Larrystilt says:
    Your comment is awaiting moderation.

    Thank you! Excellent information!

    sildenafil citrate or viagra medicare prescription plans viagra covered
    viagra online
    why do men want viagra – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  30. Larrystilt says:
    Your comment is awaiting moderation.

    You reported it perfectly.

    viagra and cancer cheap viagra on line
    viagra for women
    viagra cialis and – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  31. Larrystilt says:
    Your comment is awaiting moderation.

    Kudos! An abundance of posts.

    viagra buy it online now viagra results videos
    viagra for sale uk
    buy keyword qoclick com viagra – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  32. Larrystilt says:
    Your comment is awaiting moderation.

    Kudos! Loads of facts!

    viagra cialis levitra href viagra maxamizer
    generic viagra 100mg
    compare generic viagra prices – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  33. Larrystilt says:
    Your comment is awaiting moderation.

    Appreciate it, Loads of forum posts.

    roomid 71 viagra viagra drug scams
    viagra for women
    viagra suppliers uk – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  34. Larrystilt says:
    Your comment is awaiting moderation.

    Helpful tips. Cheers!

    redneck viagra viagra after a heart attack
    generic viagra online
    buy onlinecom phentermine viagra – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  35. Larrystilt says:
    Your comment is awaiting moderation.

    You actually said that terrifically.

    viagra otc slow down viagra
    viagra online
    long term use of viagra – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  36. Larrystilt says:
    Your comment is awaiting moderation.

    Wonderful write ups. Many thanks!

    viagra cialis levitra compared arlene farmer viagra
    cheap viagra
    global systems viagra – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

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

    Kudos! I enjoy it.

    viagra find edinburgh charles pages sildenafil womens viagra
    viagra for sale uk
    female viagra wikipedia – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  39. Larrystilt says:
    Your comment is awaiting moderation.

    Great stuff. Regards.

    smell sensitivity with viagra don’t eat with viagra
    viagra for sale uk
    woman plants viagra on co-worker – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  40. Larrystilt says:
    Your comment is awaiting moderation.

    Superb information. Appreciate it.

    tequila viagra mexico tiki bar taking viagra when not needed
    generic viagra 100mg
    viagra pro online in usa – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  41. Larrystilt says:
    Your comment is awaiting moderation.

    You have made your point!

    real viagra paypal can young people take viagra
    cheap viagra
    viagra prescription buy – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  42. Larrystilt says:
    Your comment is awaiting moderation.
  43. Larrystilt says:
    Your comment is awaiting moderation.

    Kudos, Lots of forum posts.

    viagra literotica viagra flushing
    generic viagra online
    u 2732 viagra – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  44. Larrystilt says:
    Your comment is awaiting moderation.

    Whoa a good deal of wonderful tips.

    viagra in the dominican republic viagra best use
    buy viagra
    sex viagra porn photos – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  45. Larrystilt says:
    Your comment is awaiting moderation.

    Thanks a lot! I enjoy it!

    buy cheao cgeap kamagra uk viagra viagra recipe
    viagra online
    cost of cialis vs viagra – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  46. Larrystilt says:
    Your comment is awaiting moderation.

    Nicely put, Cheers.

    ed treatments when viagra doesn’t work viagra pr cautions demploi
    cheap viagra
    viagra mail order india – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  47. Larrystilt says:
    Your comment is awaiting moderation.

    Very good content, Appreciate it.

    viagra edinburgh search cartoons charles viagra hypertension
    viagra 100mg
    written prescription for viagra – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  48. Larrystilt says:
    Your comment is awaiting moderation.

    Perfectly expressed truly. !

    free viagra 2009 jelsoft enterprises ltd india generic viagra on line sales
    viagra for women
    craigslist toronto viagra cialis – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

  49. Larrystilt says:
    Your comment is awaiting moderation.

    You actually expressed this really well.

    cheap us viagra viagra literotica
    viagra without a doctors prescription
    top selling herbal viagra – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  50. Larrystilt says:
    Your comment is awaiting moderation.

    Awesome material, Appreciate it!

    counterfeit viagra will viagra make you horney
    viagra 100mg
    best price for sildenafil generic viagra – https://genericviagrantx.com

    Geraldguh
    Dwayneamuck
    Kevindup

  51. Larrystilt says:
    Your comment is awaiting moderation.

    Info certainly utilized!!

    viagra jet lag viagra generic no prescription
    viagra 100mg
    viagra melt tabs – https://genericviagramsl.com

    Geraldguh
    Dwayneamuck
    Kevindup

  52. Larrystilt says:
    Your comment is awaiting moderation.

    You mentioned it fantastically.

    cracking knuckles viagra canada viagra office
    generic viagra online
    natural herbs instead of viagra – https://genericviagrarcp.com

    Geraldguh
    Dwayneamuck
    Kevindup

Leave a Reply

Your email address will not be published.