0% found this document useful (0 votes)
23 views4 pages

Ex03-Retrieving Data With SELECT

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Ex03-Retrieving Data With SELECT

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7/25/24, 4:29 PM ex03-Retrieving Data with SELECT.

ipynb - Colab

keyboard_arrow_down ex03-Retrieving Data with SELECT


When working with databases and SQL, the most common task is to request data from one or more tables, which returns this data in the form
of a result table. These result tables are called result-sets. The SELECT statement accomplishes this. In most applications, SELECT is the most
commonly used data query language (DQL) command. Moreover, the SELECT can do far more than simply retrieve and display data and I will
show you in the following sections.

SELECT Syntax

SELECT column1, column2, columnN FROM table_name;

Here, column1, column2... are the fields (or columns) of a table (table_name).

However, if you want query all records, just use:

SELECT * FROM table_name;

%load_ext sql

keyboard_arrow_down 1. Connet to the given database of demo.db3


%sql sqlite:///data/demo.db3

u'Connected: @data/demo.db3'

If you do not remember the tables in the demo data, you can always use the follow command to query. Here we select the table of
watershed_yearly as an example.

%sql SELECT name FROM sqlite_master WHERE type='table'

* sqlite:///data/demo.db3
Done.
name
rch
watershed_monthly
watershed_yearly
rch_info
ave monthly basin

keyboard_arrow_down 2. Retrieving all data


As we alreay know, the most common SQL operation is a SELECT statement, which pulls data from a table and then displays the results. Let’s
write our first SQL statement.

%sql SELECT * From watershed_yearly

https://colab.research.google.com/drive/1t2lENMoA4bWu8Hy3KAEv7C_-pNNTAmoD#printMode=true 1/4
7/25/24, 4:29 PM ex03-Retrieving Data with SELECT.ipynb - Colab

* sqlite:///data/demo.db3
Done.
YR PREC_mm SURQ_mm LATQ_mm GWQ_mm PERCOLA_mm TILEQ_mm SW_mm ET_mm PET_mm WYLD_mm
1981 895.605102539 130.357467651 51.3343925476 406.073944092 443.198486328 0.0 18819.7070312 236.983001709 840.687438965 609.706787109 91
1982 884.670654297 92.2126541138 50.3476905823 367.410705566 424.678375244 0.0 19425.8359375 256.79083252 872.074707031 530.024597168 64
1983 816.660522461 129.163101196 54.4730834961 445.333068848 462.763244629 0.0 18476.1953125 237.705551147 797.047363281 652.815795898 92
1984 867.57434082 95.579460144 46.9486465454 347.244842529 408.539703369 0.0 17727.7128906 223.089752197 911.256347656 510.379333496 63
1985 637.725524902 75.5576019287 45.3368835449 358.295318604 361.485229492 0.0 15840.5400391 236.321350098 990.855773926 499.339904785 49
1986 733.841247559 65.6301651001 36.7625923157 241.938842773 281.668334961 0.0 16059.4345703 243.030563354 977.513427734 358.093688965 48
1987 1007.89447021 110.286323547 62.2374191284 445.539459229 493.004302979 0.0 19541.8691406 256.957000732 841.005554199 641.211853027 76
1988 895.846618652 149.652359009 61.4932556152 450.495697021 493.116424561 0.0 17205.8496094 220.839523315 891.133300781 686.127441406 10
1989 930.10546875 111.371284485 57.6666145325 418.17276001 474.92477417 0.0 17915.3359375 208.58026123 805.931884766 609.695983887 74
1990 751.455383301 102.002082825 54.3105697632 409.75189209 435.158782959 0.0 16842.5175781 213.632339478 966.489074707 589.345031738 73
1991 984.470336914 86.0814437866 57.2631607056 446.937133789 494.857543945 0.0 17945.2539062 227.462356567 853.975219727 614.113037109 57
1992 907.946350098 147.825881958 69.509727478 557.262512207 585.333862305 0.0 17973.1796875 211.621643066 799.1640625 803.938598633 10
1993 1057.77331543 110.492515564 63.8014297485 483.817962646 531.886779785 0.0 19065.5039062 240.463851929 788.538757324 684.68737793 79
1994 802.126220703 148.677810669 52.7576904297 400.746337891 422.868255615 0.0 16865.7363281 198.036361694 857.445983887 625.035949707 11
1995 696.852783203 98.8078994751 51.8555603027 378.68737793 416.106018066 0.0 17348.3203125 161.563354492 713.622009277 549.508789062 72
1996 799.967468262 97.7275466919 44.8787841797 375.729888916 403.755828857 0.0 18504.6796875 159.914932251 652.87701416 538.928161621 71
1997 689.377502441 144.336883545 47.6871490479 331.718414307 376.208831787 0.0 16656.5117188 191.312713623 879.662658691 542.038574219 12
1998 843.460205078 81.3304672241 50.135219574 389.658203125 417.910491943 0.0 17480.8613281 187.159759521 714.47869873 542.241271973 60
1999 644.301635742 87.4329223633 39.0947494507 267.040039062 290.338745117 0.0 17345.7363281 186.385971069 848.996459961 409.593139648 70
2000 497.951629639 55.7663917542 40.3027000427 267.898681641 298.99395752 0.0 15240.1708984 176.56817627 953.658081055 378.92376709 40
2001 512.250915527 58.4281311035 37.9326858521 243.516738892 278.890472412 0.0 13922.0683594 182.05116272 1052.44824219 353.880279541 40
2002 702.02935791 51.3531570435 47.5594902039 329.117095947 377.576080322 0.0 13969.1435547 191.784317017 980.243469238 445.79989624 33
2003 729.944213867 79.3484802246 52.1592178345 367.613342285 393.827941895 0.0 16017.6982422 208.447479248 954.48248291 519.266540527 54
2004 818.378112793 79.1589050293 52.8427391052 352.751464844 412.04675293 0.0 16572.1777344 208.249572754 947.57208252 504.16885376 55
2005 855.009216309 112.442214966 55.7015419006 415.292022705 454.713256836 0.0 16346.9257812 187.91116333 857.385742188 605.73425293 79
2006 612.290344238 118.152503967 47.1709671021 325.511047363 350.234375 0.0 14916.3046875 199.269134521 961.90838623 509.445678711 85
2007 822.174682617 88.1499328613 57.7641525269 408.352386475 459.403961182 0.0 15647.421875 197.012664795 914.823669434 576.335998535 61
2008 740.08996582 57.7941246033 47.3521957397 321.425048828 355.230438232 0.0 15412.9677734 223.115386963 1074.16943359 445.341430664 38
2009 1040.90124512 128.018554688 67.974647522 485.577331543 548.677978516 0.0 17289.6113281 242.202804565 921.880187988 706.555175781 89
2010 905.668457031 126.467193604 71.5470199585 516.849060059 526.510375977 0.0 17295.3496094 259.189575195 900.288879395 743.073425293 93

keyboard_arrow_down 3. Retrieving data from specific columns


In many cases, it is not necessary to pull all columns in a SELECT statement. You can also pick and choose only the columns you are interested
in. The following query will only pull the YR, PREC_mm and PET_mm columns:

%sql SELECT YR, PREC_mm, PET_mm From watershed_yearly

https://colab.research.google.com/drive/1t2lENMoA4bWu8Hy3KAEv7C_-pNNTAmoD#printMode=true 2/4
7/25/24, 4:29 PM ex03-Retrieving Data with SELECT.ipynb - Colab

* sqlite:///data/demo.db3
Done.
YR PREC_mm PET_mm
1981 895.605102539 840.687438965
1982 884.670654297 872.074707031
1983 816.660522461 797.047363281
1984 867.57434082 911.256347656
1985 637.725524902 990.855773926
1986 733.841247559 977.513427734
1987 1007.89447021 841.005554199
1988 895.846618652 891.133300781
1989 930.10546875 805.931884766
1990 751.455383301 966.489074707
1991 984.470336914 853.975219727
1992 907.946350098 799.1640625
1993 1057.77331543 788.538757324
1994 802.126220703 857.445983887
1995 696.852783203 713.622009277
1996 799.967468262 652.87701416
1997 689.377502441 879.662658691
1998 843.460205078 714.47869873
1999 644.301635742 848.996459961
2000 497.951629639 953.658081055
2001 512.250915527 1052.44824219
2002 702.02935791 980.243469238
2003 729.944213867 954.48248291
2004 818.378112793 947.57208252
2005 855.009216309 857.385742188
2006 612.290344238 961.90838623
2007 822.174682617 914.823669434
2008 740.08996582 1074.16943359
2009 1040.90124512 921.880187988
2010 905.668457031 900.288879395

keyboard_arrow_down 4. Do some calculations in SELECT Statements


:) The SELECT statement can do far more than simply select columns.

Sometimes, we are also intersted in the relationship between columns. This can be done with expressions in SELECT Statements. For example,
I'd like to see the difference between PREC_mm and PET_mm columns (using the minus operator -). You can also try other operators such as +,
*, / or %.

%sql SELECT YR, PREC_mm, PET_mm, PET_mm-PREC_mm From watershed_yearly

https://colab.research.google.com/drive/1t2lENMoA4bWu8Hy3KAEv7C_-pNNTAmoD#printMode=true 3/4
7/25/24, 4:29 PM ex03-Retrieving Data with SELECT.ipynb - Colab

* sqlite:///data/demo.db3
Done.
YR PREC_mm PET_mm PET_mm-PREC_mm
1981 895.605102539 840.687438965 -54.9176635742
1982 884.670654297 872.074707031 -12.5959472656
1983 816.660522461 797.047363281 -19.6131591797
1984 867.57434082 911.256347656 43.6820068359
1985 637.725524902 990.855773926 353.130249023
1986 733.841247559 977.513427734 243.672180176
1987 1007.89447021 841.005554199 -166.888916016
It is amazing, is'nt it? However, the exmpression is presented as a new column name. It is not that beautiful. It is quite easy for us to give it a
1988 895.846618652 891.133300781 -4.71331787109
new name using an AS statement (this is known as an alias).
1989 930.10546875 805.931884766 -124.173583984
1990 751.455383301 966.489074707 215.033691406
1991 984.470336914
%sql SELECT YR, PREC_mm,853.975219727 -130.495117188
PET_mm, PET_mm-PREC_mm as PED_mm From watershed_yearly
1992 907.946350098 799.1640625 -108.782287598
* sqlite:///data/demo.db3
1993 1057.77331543 788.538757324 -269.234558105
Done.
1994 802.126220703 857.445983887 55.3197631836
YR PREC_mm PET_mm PED_mm
1995 696.852783203 713.622009277 16.7692260742
1981 895.605102539 840.687438965 -54.9176635742
1996 799.967468262 652.87701416 -147.090454102
1982 884.670654297 872.074707031 -12.5959472656
1997 689.377502441 879.662658691 190.28515625
1983 816.660522461 797.047363281 -19.6131591797
1998 843.460205078 714.47869873 -128.981506348
1984 867.57434082 911.256347656 43.6820068359
1999 644.301635742 848.996459961 204.694824219
1985 637.725524902 990.855773926 353.130249023
2000 497.951629639 953.658081055 455.706451416
1986 733.841247559 977.513427734 243.672180176
2001 512.250915527 1052.44824219 540.19732666
1987 1007.89447021 841.005554199 -166.888916016
2002 702.02935791 980.243469238 278.214111328
1988 895.846618652 891.133300781 -4.71331787109
2003 729.944213867 954.48248291 224.538269043
1989 930.10546875 805.931884766 -124.173583984
2004 818.378112793 947.57208252 129.193969727
1990 751.455383301 966.489074707 215.033691406
2005 855.009216309 857.385742188 2.37652587891
1991 984.470336914 853.975219727 -130.495117188
2006 612.290344238 961.90838623 349.618041992
1992 907.946350098 799.1640625 -108.782287598
2007 822.174682617 914.823669434 92.6489868164
1993 1057.77331543 788.538757324 -269.234558105
2008 740.08996582 1074.16943359 334.079467773
1994 802.126220703 857.445983887 55.3197631836
2009 1040.90124512 921.880187988 -119.021057129
1995 696.852783203 713.622009277 16.7692260742
2010 905.668457031 900.288879395 -5.37957763672
1996 799.967468262 652.87701416 -147.090454102
1997 689.377502441 879.662658691 190.28515625
1998 843.460205078 714.47869873 -128.981506348
1999 644.301635742 848.996459961 204.694824219
2000 497.951629639 953.658081055 455.706451416
2001 512.250915527 1052.44824219 540.19732666
2002 702.02935791 980.243469238 278.214111328
2003 729.944213867 954.48248291 224.538269043
2004 818.378112793 947.57208252 129.193969727
2005 855.009216309 857.385742188 2.37652587891
2006 612.290344238 961.90838623 349.618041992
2007 822.174682617 914.823669434 92.6489868164
2008 740.08996582 1074.16943359 334.079467773
2009 1040.90124512 921.880187988 -119.021057129
2010 905.668457031 900.288879395 -5.37957763672

Besides giving names to expressions using aliases, aliases can also be used to rename an existing column within the query. For example, we
rename PREC_mm to Precipitation_mm.

However, keep in mind that such an operatation does not affect the real data or change the name in the table.It only change the way you see it
on your screen. You can always try SELECT * From watershed_yearly to see the truth.

%sql SELECT YR, PREC_mm as Precipitation_mm, PET_mm, PET_mm-PREC_mm as PED_mm From watershed_yearly

* sqlite:///data/demo.db3
Done.
YR Precipitation_mm PET_mm PED_mm
1981 895.605102539 840.687438965 -54.9176635742
1982 884.670654297 872.074707031 -12.5959472656
1983 816.660522461 797.047363281 -19.6131591797
1984 867.57434082 911.256347656 43.6820068359
1985 637.725524902 990.855773926 353.130249023
1986 733.841247559 977.513427734 243.672180176
1987 1007.89447021 841.005554199 -166.888916016

https://colab.research.google.com/drive/1t2lENMoA4bWu8Hy3KAEv7C_-pNNTAmoD#printMode=true 4/4

You might also like