🗓️ SQL Challenge Day #19: Queries Quality and Percentage 🔹 Measure query performance with quality score and poor query rate! 📊 🔹 Problem: Calculate for each query_name: ✅ Quality = average(rating / position) ✅ Poor query % = percentage with rating < 3 ✅ Round both to 2 decimal places 🔹 Solution: SELECT query_name, ROUND(AVG(rating / position), 2) AS quality, ROUND(AVG(CASE WHEN rating < 3 THEN 1.0 ELSE 0.0 END) * 100, 2) AS poor_query_percentage FROM Queries WHERE query_name IS NOT NULL GROUP BY query_name; ✅ Result: Accepted 💡 Key Takeaway: **AVG(CASE WHEN...)** elegantly calculates percentages in one pass! No need for separate COUNTs – the average of 1s and 0s directly gives the proportion. 👇 Your turn: What's your go-to pattern for calculating percentages in SQL without subqueries? #SQL #LeetCode #DataEngineering #ProblemSolving #Coding #LearningInPublic #Database #DataAnalytics
SQL Challenge: Calculate Query Quality and Percentage
More Relevant Posts
-
Stop writing SQL "Inception." Use CTEs instead. 🛑📖 We’ve all been there: opening a query only to find a subquery, inside a subquery, inside another subquery. It’s hard to read, impossible to debug, and a nightmare for your future self. If you want to move from a "Junior" to a "Senior" SQL mindset, you need to master CTEs (Common Table Expressions). Why CTEs are a game-changer: Readability: They allow you to read code from top to bottom, like a story, rather than from the inside out. Reusability: You can reference the same CTE multiple times in one query. No more copy-pasting the same subquery logic! Debugging: You can test each "layer" of your data transformation individually before joining them all together. The Golden Rule: If your logic has more than two levels of nesting, it’s time for a WITH clause. #SQL #DataEngineering #Database #CodingTips #CleanCode #DataAnalytics #CareerGrowth
To view or add a comment, sign in
-
-
Most people learn SQL. Very few use it to think like an analyst. Here’s what changed that for me I was writing subqueries. They worked. But they looked like this: → Nested → Confusing → Hard to debug Then I discovered CTEs. Same logic. But suddenly… everything was clear. My query didn’t just run anymore — it started to tell a story. That’s the shift: SQL is not just a skill… It’s a way of thinking and communicating logic. #SQL #DataAnalytics #SQLTips #LearningJourney #QueryOptimization #TechCareers #CareerGrowth
To view or add a comment, sign in
-
-
Recently, I worked on a case where a query was returning incomplete results. At first glance, everything looked correct. The query was valid, no errors, and the logic seemed fine. But the output didn’t match the expected data. After digging deeper, the issue wasn’t in the query itself… 👉 It was a missing relationship between tables. The join condition didn’t cover all cases, which caused some records to be excluded. Small detail… big impact. This experience reinforced something important: 👉 Always validate relationships between tables, not just query syntax. Because in databases, structure is just as important as logic. #SQL #Database #Backend #Debugging #Engineering
To view or add a comment, sign in
-
🗓️ SQL Challenge Day #27: Biggest Single Number 🔹 Find the largest number that appears exactly once! 🔢 🔹 Problem: Return the biggest "single" number (appears only once): ✅ If no such number exists, return NULL 🔹 Solution: SELECT MAX(num) AS num FROM ( SELECT num, COUNT(1) AS cnt FROM MyNumbers GROUP BY num HAVING cnt = 1 ) t; ✅ Result: Accepted 💡 Key Takeaway: **MAX() handles NULL gracefully!** The outer query returns NULL automatically if the inner subquery finds no single numbers – no extra logic needed. This is cleaner than using CASE or IFNULL here. 👇 Your turn: What’s your go-to pattern for handling "return NULL if empty result" scenarios in SQL? #SQL #LeetCode #DataEngineering #ProblemSolving #Coding #LearningInPublic #Database #DataAnalytics
To view or add a comment, sign in
-
-
🌅 Morning with Mistakes #1: The Comma That Broke My Query Started my morning with SQL practice… and a tiny mistake cost me more time than expected 👇 ❌ What went wrong: I forgot to add a comma between columns in the SELECT statement SELECT p.project_id ROUND(AVG(e.experience_years), 2) AS average_years At first glance, it looks correct… but SQL reads it as one expression → ❌ ERROR ✅ Fixed version: SELECT p.project_id, ROUND(AVG(e.experience_years), 2) AS average_years 💡 Morning lesson: • Never forget commas between columns • Small syntax errors can break the whole query • Debugging = patience + attention to detail ☀️ Day 1 of Morning with Mistakes — learning SQL the real way! #SQL #DataAnalytics #LearningInPublic #SQLMistakes #MorningLearning
To view or add a comment, sign in
-
I’m learning that SQL errors are often not about “complex code” but about small things: query order, punctuation, capitalization, and spelling. What appears to be a logic problem is a missing comma, incorrect keyword placement, or a filter written in the wrong way. The more I practice, the more I see that understanding how SQL thinks makes debugging much easier. Two lessons stood out for me: first, SQL needs structure in the right order, especially knowing where the data is coming from before applying selections and filters. Second, filtering becomes much more powerful when you understand operators like AND, OR, BETWEEN, IN, LIKE, IS NULL, and IS NOT NULL. My biggest takeaway: when debugging SQL, start by checking syntax and query flow first, then review your filtering logic step by step. #SQL #learninginpublic #data
To view or add a comment, sign in
-
✅ Solved a SQL problem on LeetCode — Day 44 of my SQL Journey 💪 IP addresses look structured… but validating them isn’t always simple 🌐 Today’s problem was about identifying invalid IP addresses — not just formatting issues, but logical errors inside each segment. I used string functions and conditional checks to: • Split IP into individual octets using string operations • Validate the number of segments (must be 4) • Check if each octet is within the valid range (0–255) • Detect non-numeric values using pattern matching • Count how often each invalid IP appears What I practised: • Working with string parsing in SQL • Using SUBSTRING_INDEX() for segment extraction • Applying REGEXP for pattern validation • Combining multiple validation rules in one query What stood out — Validation isn’t just about format… it’s about the rules behind the format. A value can look correct, but still logically invalid. That’s where careful checking matters. SQL isn’t just for analysis. It can also enforce data quality. Consistent learning, one query at a time 🚀 #SQL #LeetCode #SQLPractice #DataAnalytics #LearningInPublic
To view or add a comment, sign in
-
-
Stop jumping between SQL topics Follow a clear path This roadmap shows how to go from zero to advanced SQL ⬇️ Step 1 Basics • What is SQL • Tables and databases • Data types and NULL • CRUD operations • DDL vs DML ⬇️ Step 2 Queries • SELECT • WHERE with AND OR NOT • ORDER BY • GROUP BY • LIMIT and DISTINCT ⬇️ Step 3 Functions • COUNT SUM AVG MIN MAX • UPPER LOWER CONCAT • Date functions • COALESCE ⬇️ Step 4 Joins • INNER • LEFT • RIGHT • FULL • SELF • CROSS ⬇️ Step 5 Subqueries • SELECT FROM WHERE • Correlated queries ⬇️ Step 6 Constraints • PRIMARY KEY • FOREIGN KEY • UNIQUE • NOT NULL • CHECK ⬇️ Step 7 Indexes and views • Index basics • Performance tradeoffs • Views ⬇️ Step 8 Normalization • 1NF 2NF 3NF • Remove redundancy • When to denormalize ⬇️ Step 9 Transactions • BEGIN COMMIT ROLLBACK • ACID • Isolation levels ⬇️ Step 10 Advanced SQL • Window functions • CTEs • Stored procedures • Triggers ⬇️ Step 11 Practice • Build projects • Prepare for interviews • Optimize queries Rule Learn then apply immediately #SQL #DataAnalytics #LearnSQL #Database #ProgrammingValley
To view or add a comment, sign in
-
-
🚀 𝗦𝗤𝗟 𝗝𝗼𝘂𝗿𝗻𝗲𝘆 – 𝗗𝗮𝘆 𝟮𝟲: 𝗜𝗻𝘁𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝗼𝗻 𝘁𝗼 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝗶𝗲𝘀 Today I explored one of the most powerful concepts in SQL — Subqueries 🔍 🔹 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗮 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝘆? A subquery is a query inside another query used to perform intermediate calculations and filter results. 💼 𝗥𝗲𝗮𝗹-𝘄𝗼𝗿𝗹𝗱 𝗽𝗲𝗿𝘀𝗽𝗲𝗰𝘁𝗶𝘃𝗲 (𝗠𝗮𝗻𝗮𝗴𝗲𝗿’𝘀 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀): Which product performs well? Which store performs well? Which customers perform well? 👉 These types of questions are easily solved using subqueries by comparing individual performance with averages or totals. 🔹 𝗖𝗼𝗿𝗿𝗲𝗹𝗮𝘁𝗲𝗱 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝘆 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: SELECT cid, cname, category, amt FROM cust c1 WHERE amt > ( SELECT AVG(amt) FROM cust c2 WHERE c2.category = c1.category ); ✔ Here, the inner query depends on the outer query ✔ It runs for each row → making it a correlated subquery 🔹 𝗧𝘆𝗽𝗲𝘀 𝗼𝗳 𝗦𝘂𝗯𝗾𝘂𝗲𝗿𝗶𝗲𝘀 (𝗢𝘃𝗲𝗿𝘃𝗶𝗲𝘄): 📌 𝗕𝗮𝘀𝗲𝗱 𝗼𝗻 𝗥𝗲𝘀𝘂𝗹𝘁 𝗧𝘆𝗽𝗲: Scalar Subquery (single value) Row Subquery Table Subquery 📌 𝗕𝗮𝘀𝗲𝗱 𝗼𝗻 𝗗𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝗰𝘆: Correlated Subquery Non-Correlated Subquery 📌 𝗕𝗮𝘀𝗲𝗱 𝗼𝗻 𝗟𝗼𝗰𝗮𝘁𝗶𝗼𝗻: SELECT clause FROM clause WHERE clause 📌 𝗕𝗮𝘀𝗲𝗱 𝗼𝗻 𝗞𝗲𝘆𝘄𝗼𝗿𝗱𝘀 𝗨𝘀𝗲𝗱: IN EXISTS ANY / ALL 💡 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆: Subqueries help in breaking complex problems into simpler parts and are widely used in real-world data analysis. 🔥 Day by day, getting closer to mastering SQL! #SQL #LearningJourney #DataAnalytics #Subqueries #Coding #PlacementPreparation #40DaysOfCode
To view or add a comment, sign in
-
-
SQL is the backbone of data work, but even the pros forget the specific order of a WINDOW FUNCTION or the difference between UNION and UNION ALL sometimes. I put together this comprehensive SQL Cheat Sheet to keep all the essentials in one place—from basic DML to advanced CTEs and Triggers. ✅ What’s inside: Joins & Set Operations Window Functions & Aggregations Date/Time formatting Transaction control Save this post 📌 so you can find it during your next project, and Tag a friend who’s currently learning SQL! #SQL #DataScience #DataAnalytics #Coding #Database #ProgrammingTips
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development