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

    Cheers, Valuable information.

    hours viagra funny picture viagra
    viagra 100mg
    viagra light switch cover plate – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  4. ErnestoAnals says:
    Your comment is awaiting moderation.

    Thanks a lot! Terrific information!

    viagra cheap uk buy purchase viagra operation
    viagra without a doctors prescription
    viagra maximum dosage – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  5. ErnestoAnals says:
    Your comment is awaiting moderation.

    Thanks a lot! Quite a lot of write ups!

    discounts on viagra time to take effect viagra
    buy viagra
    viagra belgique – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  6. ErnestoAnals says:
    Your comment is awaiting moderation.

    Kudos! I value this!

    viagra online without prescription effects of viagra on females
    viagra pills
    interactions of different medications with viagra – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  7. ErnestoAnals says:
    Your comment is awaiting moderation.

    You actually said it very well.

    female viagra fda approved products viagra best price on net scam
    buy viagra online
    l-arginine comparable to viagra – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  8. ErnestoAnals says:
    Your comment is awaiting moderation.

    You stated that perfectly.

    viagra price costco how to get viagra fast
    generic viagra
    by money order viagra – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  9. ErnestoAnals says:
    Your comment is awaiting moderation.

    Amazing lots of excellent knowledge.

    viagra hyper viagra and cataracks
    viagra without a doctor prescription
    epinephrine viagra contradiction – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  10. ErnestoAnals says:
    Your comment is awaiting moderation.

    Very good posts. Appreciate it!

    robin william viagra girl takes viagra
    viagra for women
    viagra adds to my phone – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  11. ErnestoAnals says:
    Your comment is awaiting moderation.

    With thanks. An abundance of content.

    lowest dosage of viagra viagra withour prescription
    viagra without a doctor prescription
    does viagra do to a girl – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  12. ErnestoAnals says:
    Your comment is awaiting moderation.

    Many thanks, Ample info!

    viagra for altitude natural sources viagra
    viagra for women
    viagra pharmacy uk – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  13. ErnestoAnals says:
    Your comment is awaiting moderation.

    You made your position very nicely..

    viagra 100 pic viagra professonal
    generic viagra online
    regalis and viagra – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  14. ErnestoAnals says:
    Your comment is awaiting moderation.

    Thanks a lot. Plenty of info!

    edinburgh 17 viagra pages find search find local charles linskaill search viagra
    buy viagra
    viagra pro pharmacy complaints – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  15. ErnestoAnals says:
    Your comment is awaiting moderation.
  16. ErnestoAnals says:
    Your comment is awaiting moderation.

    Amazing loads of excellent facts!

    where to get viagra rachael ray womens viagra
    viagra for women
    apcalis levitra viagra – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  17. ErnestoAnals says:
    Your comment is awaiting moderation.

    This is nicely put. !

    doctors north central wisconsin viagra viagra in the water chords
    cheap viagra
    discount buy viagra – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  18. ErnestoAnals says:
    Your comment is awaiting moderation.

    Thank you! Numerous forum posts!

    if viagra dose not work pros and cons of taking viagra
    viagra online
    viagra buyer – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  19. ErnestoAnals says:
    Your comment is awaiting moderation.

    Seriously all kinds of fantastic knowledge!

    does viagra last longer viagra compare price
    buy viagra
    viagra headquarters in canada – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  20. ErnestoAnals says:
    Your comment is awaiting moderation.

    You definitely made your point!

    does generic viagra less sildenafil citrate i want to buy viagra
    buy generic viagra
    buy viagra in malaysia – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  21. ErnestoAnals says:
    Your comment is awaiting moderation.

    Amazing loads of fantastic information!

    viagra related to hair growth viagra unsuccessful entry pdf
    generic viagra 100mg
    generic money order viagra – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  22. ErnestoAnals says:
    Your comment is awaiting moderation.

    You actually mentioned it adequately.

    viagra triangle westlake ohio cialis versus levitra versus viagra
    viagra for women
    levitra viagra cialis – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  23. ErnestoAnals says:
    Your comment is awaiting moderation.

    You’ve made your position extremely well..

    viagra induced erection live celebrity endorsments viagra
    viagra without a doctors prescription
    cheap viagra discount – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  24. ErnestoAnals says:
    Your comment is awaiting moderation.

    Kudos. I appreciate this!

    viagra addiction zapiro cartoon viagra
    generic viagra online
    taking viagra with beta blockers – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  25. ErnestoAnals says:
    Your comment is awaiting moderation.

    You explained this well.

    cheap generic viagra worcester imc cod generic pal pay viagra
    buy viagra online
    viagra and food – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  26. ErnestoAnals says:
    Your comment is awaiting moderation.

    You actually reported that adequately!

    does viagra work online viagra without prescription paypal
    buy viagra
    prices publix viagra – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  27. ErnestoAnals says:
    Your comment is awaiting moderation.

    Nicely spoken certainly. !

    cheapest genuine viagra tablets viagra no prior prescription
    viagra for sale uk
    sites for charles linskaill viagra find – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  28. ErnestoAnals says:
    Your comment is awaiting moderation.

    You suggested it very well.

    viagra drug interactions and side effects the effects of too much viagra
    viagra without a doctor prescription
    is viagra legal in japan – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  29. ErnestoAnals says:
    Your comment is awaiting moderation.

    Thanks. Quite a lot of facts.

    buy viagra from canada cheapest uk supplier viagra
    viagra for sale uk
    can you buy viagra without prescripition – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  30. ErnestoAnals says:
    Your comment is awaiting moderation.

    You actually explained that superbly.

    viagra 6 free sample uaw union gets viagra
    buy viagra online
    viagra story – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  31. ErnestoAnals says:
    Your comment is awaiting moderation.

    With thanks. I value this.

    counterfeit viagra online viagra boots
    buy generic viagra
    proper use of viagra – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  32. ErnestoAnals says:
    Your comment is awaiting moderation.

    Very good advice. Cheers!

    uk based suppliers generic viagra making the most of viagra
    viagra 100mg
    viagra profesional – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  33. ErnestoAnals says:
    Your comment is awaiting moderation.

    Valuable info. Appreciate it.

    viagra business compare viagra with kamagra
    viagra for sale uk
    generic online order viagra – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  34. ErnestoAnals says:
    Your comment is awaiting moderation.

    You have made your stand pretty well!!

    increase cost of viagra viagra genric
    generic viagra
    sex after taking viagra – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  35. ErnestoAnals says:
    Your comment is awaiting moderation.

    Useful data. Appreciate it!

    viagra 100mg 90 pills viagra stuffy nose
    cheap viagra
    female viagra fox news – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  36. ErnestoAnals says:
    Your comment is awaiting moderation.

    Nicely put, Many thanks.

    generic viagra forum watermelon viagra substitute
    viagra for sale uk
    viagra free sites computer search buy – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  37. ErnestoAnals says:
    Your comment is awaiting moderation.

    Valuable material. Thanks a lot.

    how to ask doctor for viagra viagra brisbane lions
    generic viagra online
    okean viagra mp3 – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  38. ErnestoAnals says:
    Your comment is awaiting moderation.

    Thanks a lot. Lots of info!

    viagra and antidepressents press releases coma viagra
    viagra for sale uk
    emphysema primary pulmonary hypertension viagra – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  39. ErnestoAnals says:
    Your comment is awaiting moderation.

    Appreciate it! A good amount of knowledge!

    rapid release viagra tablets get viagra prescription online
    viagra without a doctors prescription
    ambien gift viagra – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  40. ErnestoAnals says:
    Your comment is awaiting moderation.
  41. ErnestoAnals says:
    Your comment is awaiting moderation.

    Truly many of amazing knowledge.

    best places to buy viagra kamagra generic viagra soft 100
    buy viagra online
    altermatives to viagra – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

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

    Fine stuff. Regards!

    viagra results pics viagra and beta blockers
    viagra online
    viagra indigestion – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  44. ErnestoAnals says:
    Your comment is awaiting moderation.

    You reported this perfectly!

    does alka seltzer enhance viagra drug classification for viagra
    viagra for sale uk
    can nitroglycerin be used like viagra – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  45. ErnestoAnals says:
    Your comment is awaiting moderation.

    Kudos, Loads of facts!

    hom to viagra 2003 apcalis levitra market sales viagra
    viagra pills
    order uk viagra – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  46. ErnestoAnals says:
    Your comment is awaiting moderation.

    Thank you. Great stuff.

    generic levitra cialis viagra cialis compar compar levitra viagra
    viagra 100mg
    dog eats viagra – https://genericviagrantx.com

    KeithAsype
    Richardraf
    ErnestoAnals

  47. ErnestoAnals says:
    Your comment is awaiting moderation.

    This is nicely expressed. !

    generic brands of viagra viagra cialis comparison
    buy viagra online
    viagra falls 2 xxx – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  48. ErnestoAnals says:
    Your comment is awaiting moderation.

    Amazing loads of terrific knowledge.

    acheter du viagra viagra in cction
    viagra online
    hero tabs versus viagra – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  49. ErnestoAnals says:
    Your comment is awaiting moderation.

    Nicely put. Kudos!

    viagra delivered on saturday by fedex viagra without prescription new york
    generic viagra
    viagra television commercial – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  50. ErnestoAnals says:
    Your comment is awaiting moderation.

    Information well considered..

    viagra prescription low cost viagra generic mastercard precription
    viagra without a doctors prescription
    take cialis and viagra together – https://genericviagrarcp.com

    KeithAsype
    Richardraf
    ErnestoAnals

  51. ErnestoAnals says:
    Your comment is awaiting moderation.

    Perfectly spoken of course. !

    viagra pill picture viagra blog posts
    buy viagra
    super viagra – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  52. ErnestoAnals says:
    Your comment is awaiting moderation.

    Appreciate it. Lots of information.

    prescription female viagra viagra generic soft tab
    buy viagra
    usual dosage of viagra – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  53. ErnestoAnals says:
    Your comment is awaiting moderation.

    Whoa many of helpful information.

    generic viagra manufacturer order viagra here
    viagra without a doctors prescription
    viagra and pulmonary hypertension – https://genericviagramsl.com

    KeithAsype
    Richardraf
    ErnestoAnals

  54. ErnestoAnals says:
    Your comment is awaiting moderation.

Leave a Reply

Your email address will not be published.