SQL functions useful for Data analysis _ Towards Dev
SQL functions useful for Data analysis _ Towards Dev
72 4
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 1/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
In the realm of SQL, there are several advanced functions that often remain
underutilized despite their potential to streamline data analysis, improve
performance, and enable more sophisticated queries. Let’s delve into some
of these lesser-known SQL functions that can elevate your data analysis
capabilities:
Lets begin
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 2/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
1. Window Functions:
Window functions allow you to perform calculations across a set of rows
related to the current row. These functions can significantly simplify
complex queries that involve ranking, cumulative sums, moving averages,
and more.
SELECT
date,
revenue,
SUM(revenue) OVER (ORDER BY date) AS running_total
FROM
sales_data;
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 3/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
3. JSON Functions
Modern databases support JSON data types, and SQL provides functions to
manipulate and query JSON data directly within your queries.
SELECT
json_data->>'$.name' AS name,
json_data->>'$.age' AS age
FROM
json_table;
4. STRING_AGG Function
STRING_AGG allows you to concatenate values from multiple rows into a
single string, useful for generating comma-separated lists or concatenated
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 4/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
text.
SELECT
department,
STRING_AGG(employee_name, ', ') AS employees
FROM
employee_data
GROUP BY
department;
SELECT
date,
revenue,
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 5/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
SELECT
customer_id,
AVG(revenue) FILTER (WHERE revenue > 1000) AS avg_high_value_revenue
FROM
sales_data
GROUP BY
customer_id;
7. ARRAY Functions
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 6/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
SELECT
user_id,
ARRAY_INTERSECT(purchased_items, recommended_items) AS common_items
FROM
user_purchase_history;
SELECT
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY score) AS median,
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY score) AS 75th_percentile
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 7/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
FROM
exam_scores;
9. STRING_TO_ARRAY
When working with arrays, these functions facilitate converting strings to
arrays and vice versa, enabling more versatile data manipulation.
SELECT
student_name,
STRING_TO_ARRAY(subjects, ', ') AS subject_list
FROM
student_subjects;
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 8/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
SELECT
customer_name,
REGEXP_SUBSTR(contact_info, '\d{3}-\d{3}-\d{4}') AS phone_number
FROM
customer_contacts;
SELECT
timestamp,
value,
AVG(value) OVER (
ORDER BY timestamp
RANGE BETWEEN INTERVAL '1 day' PRECEDING AND CURRENT ROW
) AS moving_average
FROM
sensor_data;
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 9/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
WITH sales_summary AS (
SELECT
date,
SUM(revenue) AS daily_revenue
FROM
sales_data
GROUP BY
date
)
SELECT
date,
daily_revenue,
AVG(daily_revenue) OVER (
ORDER BY date
RANGE BETWEEN INTERVAL '7 days' PRECEDING AND CURRENT ROW
) AS 7_day_avg_revenue
FROM
sales_summary;
SELECT
customer_id,
AVG(order_amount) AS avg_order_amount,
MIN(order_amount) AS min_order_amount
FROM
orders
GROUP BY
customer_id
HAVING
MAX(order_amount) - MIN(order_amount) > 100;
Happy Learning
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 11/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 12/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
Exploring Data!!
82 281 3
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 13/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
265 99 1
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 14/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
206 3 486 5
Lists
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 15/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
31 91
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 16/17
9/19/23, 6:52 PM SQL functions useful for Data analysis | Towards Dev
Data Engineer Morgan Stanley Spotify ETL using Python and AWS
Interview Experience This arcticle focuses on the steps I took and
Morgan Stanley is a leading global investment the valuable insights I gained during my…
bank and wealth management firm and it…
101 2 22
https://towardsdev.com/exploring-lesser-known-sql-functions-for-data-analysis-5cb007c93e44 17/17