The SQL Functions That Will Get You a Raise This Year
Are your SQL skills stuck in first gear? Do you spend your days writing SELECT * FROM...
queries to export data into Excel? That’s a start, but it won’t get you noticed. To truly increase your worth—and your paycheck—you need to move beyond pulling data and start delivering sophisticated, business-critical insights directly from the database.
It’s a two-step process. First, you master the fundamentals that answer 90% of business questions. Then, you layer in more advanced functions that answer the complex questions, the ones that drive strategy and reveal hidden truths in the data.
This guide will walk you through both steps. We’ll start with the essentials and then show you the “next-level” functions that will make you indispensable.
The Mindset Shift: From Data Puller to Indispensable Analyst
First, a crucial mindset shift. Businesses don’t want data; they want answers. They are drowning in raw logs and transaction records. Your value comes from your ability to distill this noise into a clear, actionable story.
- Level 1 (The Foundation): Using basic aggregates to summarize data.
- Level 2 (The Promotion): Using advanced and window functions to provide context, comparisons, and nuanced analysis without ever leaving the database.
Mastering Level 2 is what separates the data janitor from the data scientist. Let’s get you there.
Step 1: Master the Foundation, Then Level Up
COUNT()
: Moving From “How Many?” to “How Many Uniques?”
-
The Foundational Question: “How many sales did we have last month?”
SQL-- Level 1: A simple count of all rows. SELECT COUNT(order_id) AS total_orders FROM sales WHERE sale_date >= '2025-06-01';
-
The Next-Level Question: “That’s great, but how many individual customers actually purchased from us?” This is a much more valuable metric.
The “level up” is
COUNT(DISTINCT ...)
. It differentiates between raw activity and actual customer reach.SQL-- Level 2: Counting unique entities. SELECT COUNT(DISTINCT customer_id) AS unique_customers FROM sales WHERE sale_date >= '2025-06-01';
-
How to Frame It for Your Boss: “We had 15,000 transactions last month, driven by 8,250 unique customers. This gives us an average purchase frequency of 1.8 orders per customer.”
SUM()
: Moving From a Grand Total to a Running Total
-
The Foundational Question: “What were our total sales in Q2?”
SQL-- Level 1: A single, static number. SELECT SUM(order_total) AS total_revenue FROM sales WHERE quarter = 'Q2';
-
The Next-Level Question: “How did our revenue build up over the quarter? I want to see the cumulative growth week by week.”
The “level up” is using a Window Function, specifically
SUM() OVER (...)
. This lets you calculate a running total alongside your regular data, without collapsing rows.SQL-- Level 2: Calculating a running total to show momentum. SELECT sale_week, SUM(weekly_revenue) AS weekly_revenue, SUM(SUM(weekly_revenue)) OVER (ORDER BY sale_week) AS cumulative_revenue FROM weekly_sales_summary WHERE quarter = 'Q2' GROUP BY sale_week;
-
How to Frame It for Your Boss: “Our total Q2 revenue was $1.17M. Here’s the weekly breakdown showing our growth trajectory; you can see we gained significant momentum after the mid-quarter marketing push.”
MIN()
/ MAX()
: Moving From Extremes to Meaningful SLAs
-
The Foundational Question: “What was our fastest and slowest support ticket resolution time?”
SQL-- Level 1: Finding the absolute best and worst case. SELECT MIN(resolution_time_hours) AS fastest, MAX(resolution_time_hours) AS slowest FROM support_tickets;
-
The Next-Level Question: “The max time is a single outlier that skews our perception. What is a realistic performance promise we can make to customers? What is our 95th percentile resolution time?”
The “level up” is
PERCENTILE_CONT()
. This statistical function is immune to single outliers and gives a much more accurate picture of your operational performance. It’s how modern SLAs (Service Level Agreements) are defined.SQL-- Level 2: Calculating the 95th percentile for a realistic SLA. SELECT PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY resolution_time_hours) AS p95_resolution_time FROM support_tickets;
-
How to Frame It for Your Boss: “While one ticket took 90 hours, 95% of all support requests are resolved in under 18 hours. We can confidently promise customers a resolution within 24 hours.”
The Power Move: The Multi-Layered Analysis
Now, let’s combine these concepts into a single query that delivers a truly strategic analysis—the kind that gets you noticed in a leadership meeting.
The Business Scenario: The Head of Product wants to understand the user experience for different subscription tiers. Are premium users getting better performance?
WITH user_metrics AS (
SELECT
user_id,
subscription_tier,
request_duration_ms,
-- Level 2: Get the average duration for each tier to compare against.
AVG(request_duration_ms) OVER (PARTITION BY subscription_tier) AS avg_tier_duration,
-- Level 1: Concatenate all features a user accessed into a single line.
STRING_AGG(feature_used, ', ') AS features_used_list
FROM app_logs
WHERE event_date > '2025-06-01'
GROUP BY user_id, subscription_tier, request_duration_ms
)
SELECT
subscription_tier,
COUNT(DISTINCT user_id) AS unique_users,
AVG(request_duration_ms) AS overall_avg_duration_ms,
-- Level 2: Calculate the P90 to find the "slow" experience for most users.
PERCENTILE_CONT(0.90) WITHIN GROUP (ORDER BY request_duration_ms) AS p90_duration_ms
FROM user_metrics
GROUP BY subscription_tier;
How to Frame This Analysis for Your Boss:
“I’ve analyzed app performance across our subscription tiers for June. Here’s the story:
- Performance: The ‘Premium’ tier has an average response time of 120ms, which is 40% faster than the ‘Free’ tier’s average of 200ms.
- Reliability: More importantly, the 90th percentile response time for Premium users is 250ms, whereas Free users experience a P90 of over 500ms. This confirms our premium infrastructure is providing a more consistent and reliable experience.
- Usage: By looking at the features used (using
STRING_AGG
), we can also see that premium users are engaging more with our high-value features.
This data strongly supports that our tier system is working as designed and provides a clear value proposition for users to upgrade.”
From Theory to Tangible Skill
You now have the roadmap. You’ve seen how to graduate from basic functions like COUNT
and SUM
to their more powerful, insightful cousins like COUNT(DISTINCT)
, PERCENTILE_CONT
, and window functions. You understand that this is the path from being a data retriever to becoming an indispensable analyst who drives strategy.
But knowledge without practice is temporary. Reading about these queries is one thing; seeing them transform a real dataset is another. So, what’s the biggest barrier to practice? You can’t exactly run experimental window functions on your company’s live production database. And setting up a local database server can be a complex, frustrating chore.
This is where a real-world sandbox becomes essential. To truly master these skills, you need a professional-grade environment where you can build, break, and query without consequence.
To help you make that leap from theory to practice, our friends at DigitalOcean are offering readers $200 in free credit to use over 60 days.
With this credit, you can spin up a fully managed PostgreSQL or MySQL database in just a few minutes. There’s no complex installation; you can load it with sample data and immediately start running the exact queries we’ve discussed today. You can test a running SUM()
, find a 95th percentile, and see for yourself how these commands perform on a real database.
Stop reading, and start building. Claim your $200 credit and run your first advanced query in the next 10 minutes. Your future self will thank you.