ALTERTABLE PETSALEALTERCOLUMN PET SETDATATYPEVARCHAR(20);
SELECT*FROM PETSALE;
5 records
ID
PET
SALEPRICE
SALEDATE
QUANTITY
1
Cat
450.09
2018-05-29
9
2
Dog
666.66
2018-06-01
NA
3
Parrot
50.00
2018-06-04
NA
4
Hamster
60.60
2018-06-11
NA
5
Goldfish
48.48
2018-06-14
NA
RENAME COLUMN:
ALTERTABLE PETSALERENAMECOLUMN PET TO ANIMAL;
SELECT*FROM PETSALE;
5 records
ID
ANIMAL
SALEPRICE
SALEDATE
QUANTITY
1
Cat
450.09
2018-05-29
9
2
Dog
666.66
2018-06-01
NA
3
Parrot
50.00
2018-06-04
NA
4
Hamster
60.60
2018-06-11
NA
5
Goldfish
48.48
2018-06-14
NA
4.2.3 Truncate
The TRUNCATE statement will remove all(!) rows from an existing table, just like the one we created in the beginning, however, it does not delete the table itself.
TRUNCATETABLE PET IMMEDIATE;
Caution: DROP TABLE tablename; drops the entire table!
DROPTABLE PETSALE;
4.3 Calculations
For this subsection I created a simple table that contains attributes about countries:
SELECT*FROM df ;
4 records
country
y
z
id
Germany
2
3.6
1
Austria
1
8.5
2
Brazil
4
2.5
3
Brazil
3
3.5
3
4.3.1 Like
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator which are percent sign(%) and underscore sign (_).
SELECT*FROM df WHERE country LIKE'A%';
1 records
country
y
z
id
Austria
1
8.5
2
4.3.2 Between
Select data within a range:
select*from mtcarswhere (hp BETWEEN100and200) and AM =1 ;
5 records
mpg
cyl
disp
hp
drat
wt
qsec
vs
am
gear
carb
21.0
6
160.0
110
3.90
2.620
16.46
0
1
4
4
21.0
6
160.0
110
3.90
2.875
17.02
0
1
4
4
30.4
4
95.1
113
3.77
1.513
16.90
1
1
5
2
19.7
6
145.0
175
3.62
2.770
15.50
0
1
5
6
21.4
4
121.0
109
4.11
2.780
18.60
1
1
4
2
4.3.3 In
Use the IN operator to select observations that match the provided list of the IN operator:
SELECT*FROM df WHERE country IN ('Brazil');
2 records
country
y
z
id
Brazil
4
2.5
3
Brazil
3
3.5
3
4.3.4 Order by
ORDER BY is used to sort the result-set in ascending or descending order. The default is ascending.
SELECT*FROM df ORDERBY country;
4 records
country
y
z
id
Austria
1
8.5
2
Brazil
4
2.5
3
Brazil
3
3.5
3
Germany
2
3.6
1
4.3.5 Distinct
Exclude duplicates with DISTINCT:
SELECTdistinct(country) FROM df ;
3 records
country
Germany
Austria
Brazil
4.3.6 Group by
GROUP BY is used in collaboration with SELECT to arrange identical data into groups.
SELECT country, count (country) AS Var_Name from df groupby country;
3 records
country
Var_Name
Austria
1
Brazil
2
Germany
1
4.3.7 Average
SELECT country, AVG(z) as Mean from df groupby country;
3 records
country
Mean
Austria
8.5
Brazil
3.0
Germany
3.6
4.3.8 Having
The WHEREclause is for entire result set; while HAVING works only for the GROUPED BY clause.
selectsum(mpg) as sum_mpg from mtcars where hp >100
1 records
sum_mpg
401.4
Round to the nearest integer:
selectround(drat, 1) as round_drat from mtcars
Displaying records 1 - 10
round_drat
3.9
3.9
3.9
3.1
3.1
2.8
3.2
3.7
3.9
3.9
The length of a string:
selectlength(country) from df
4 records
length(country)
7
7
6
6
Depending the SQL database you use, in db2 you can use the upper (UCASE) and lower case (LCASE) function for strings.
selectupper(country) from df
4 records
upper(country)
GERMANY
AUSTRIA
BRAZIL
BRAZIL
In case of Oracle the functions are called lower and upper.
4.3.10 Date and Time Built-in Functions
Talking about SQL databases, there are three different possibilities to work with date and time DB2.
- Date: YYYYMMDD (Year/Month/Day) - Time: HHMMSS (Hours/Min/Sec) - Timestamp: YYYYMMDDHHMMSSZZZZZZ (Date/Time/Microseconds)
Depending on what you are up to do, there are functions to extract the day, month, day of month, day of week, day of year, week, hour, minute, and second. You can also extract the current_date and the current_time. Unfortunately, this does not work in Oracle the same way as in DB2, but to give you an example how to extract the day:
selectday(date) from df where country ='Germany'
4.3.11 Working with Multiple Tables
There are several ways to access multiple tables in the same query. Namely, using sub-queries, implicit join, and join operators, such as INNER JOIN and OUTER JOIN. For instance:
select*from df2;
2 records
country
valid
id
Germany
1
1
Austria
0
2
Let’s say we want only observations from df that are listed in df2. In such a situation we can use a sub-queries:
select*from dfwhere country in (select country from df2)
2 records
country
y
z
id
Germany
2
3.6
1
Austria
1
8.5
2
Of course, you could add also information of the second table and include only countries with a certain value:
select*from dfwhere country in (select country from df2 where valid =1)
1 records
country
y
z
id
Germany
2
3.6
1
Implicit joins implies that we can access multiple tables by specifying them in the FROM clause of the query. This leads to a CROSS JOIN (also known as Cartesian Join).
select*from df, df2
8 records
country
y
z
id
country
valid
id
Germany
2
3.6
1
Germany
1
1
Germany
2
3.6
1
Austria
0
2
Austria
1
8.5
2
Germany
1
1
Austria
1
8.5
2
Austria
0
2
Brazil
4
2.5
3
Germany
1
1
Brazil
4
2.5
3
Austria
0
2
Brazil
3
3.5
3
Germany
1
1
Brazil
3
3.5
3
Austria
0
2
In DBL2 we can use the where clause to match data (see code); in Oracle there are other matching operaters
select*from df, df2 where df.id= df.id;
In case of long names, we can use shorter aliases for table names (or use column names with aliases in the SELECT clause):