Dbas Workingwithfunctions en
Dbas Workingwithfunctions en
Database Fundamentals
© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
What you will learn
2 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Functions
© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Built-in functions
Some common functions include aggregate functions, conversion functions, date functions, string functions,
mathematical functions, and control flow and window functions.
4 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Built-in functions: Example syntax
The CURRENT_DATE() function returns the current date as a value in ‘YYYY-MM-DD’ format.
5 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Built-in functions: Another example syntax
The DATE_ADD() function adds a time or date interval to a date and returns a value.
Query
Output
6 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Aggregate functions
© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Common aggregate functions
8 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Aggregate functions: Example syntax
Query Aggregate function name
Output
Total Number of Rows
--------------------
984
9 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Aggregate functions: Example syntax (continued)
Query
Output
AVG(LifeExpectancy)
------------------- Aggregate function return value
72.51200
10 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity
© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
In this activity: Time: 10 mins
Activity: Using the SUM • AnyCompany Publishing House Country
function is examining their country
table. Column Headings Type
• Discuss how to query the
country table by using the SUM Code Character
function. Feel free to annotate Name Character
your findings to begin your
discussion. Continent Character
Region Character
To do: SurfaceArea Float
• Review the column headings, IndepYear Integer
and annotate which columns
you could use with the SUM Population Integer
function.
LifeExpectancy Float
• Create a query that sums the
total population for all of the GNP Float
countries.
GNPOId Float
• After creating your list and
query, be ready to explain your LocalName Character
findings to the class.
Capital Character
Code2 Character
Hint: Review slides 7–9 for information about
12 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. aggregate functions.
DISTINCT keyword
© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
DISTINCT (different) keyword
Query
DISTINCT keyword Column names
Output
CountryCode District
----------- ----------------
BRA Distrito Federal
BRA Rio de Janeiro
BRA Sao Paulo Distinct (unique) value combinations
CHN Guangdong for the selected columns
CHN Shanghai
GBR England
GBR Scotland
14 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
DISTINCT in a COUNT function
Query DISTINCT keyword
Output
15 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Character strings and string functions
© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
String function: CHAR_LENGTH()
Query
String parameter
Returns the length of the string
measured in characters
Output
CHAR_LENGTH(‘District’)
-----------------------
8
17 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
String function: INSERT()
Query
String to be modified. String to be inserted
18 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Leading and trailing spaces in a string
Extra spaces in a string can cause issues when querying for specific data.
T E X T
Leading space Character string data Trailing space
19 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
TRIM functions: RTRIM() and LTRIM()
• The RTRIM() function removes blank spaces to the right of a string.
• The LTRIM() function removes blank spaces to the left of a string.
Query
RTRIM removes blank spaces on the right end of the string data values.
Output
ID District
----- -------- No spaces to the right of the string values
1024 Maharashtra for the District column
2331 Seoul
3793 New York
20 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Checkpoint questions
21 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Key takeaways
• SQL has built-in functions. Common functions include the
following:
– Aggregate functions
– String functions
– Date functions
22 © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Thank you
© 2022 Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from Amazon Web
Services, Inc. Commercial copying, lending, or selling is prohibited. Corrections, feedback, or other questions? Contact us at https://support.aws.amazon.com/#/contacts/aws-training. All
trademarks are the property of their owners.