🎄 SQL Advent Calendar Challenge - Day 10! 🎄 It’s the season of resolutions, and we all know how tricky it can be to stick to them. Day 10 of the SQL Advent Calendar brought an interesting challenge: tracking my friends’ New Year’s resolution progress. 📊 The task? Calculate the number of resolutions made, completed, and determine the success percentage for each friend. Then assign a success category: ✅ Green (75%+ success), 🟡 Yellow (50–75%), or 🔴 Red (below 50%). Here’s the SQL query I used to tackle the problem: SELECT friend_name, COUNT(resolution_id) AS resolutions_made, SUM(CASE WHEN is_completed = 1 THEN 1 ELSE 0 END) AS resolutions_completed, ROUND(SUM(CASE WHEN is_completed = 1 THEN 1 ELSE 0 END) * 100.0 / COUNT(resolution_id), 2) AS success_percentage, CASE WHEN SUM(CASE WHEN is_completed = 1 THEN 1 ELSE 0 END) * 100.0 / COUNT(resolution_id) > 75 THEN 'Green' WHEN SUM(CASE WHEN is_completed = 1 THEN 1 ELSE 0 END) * 100.0 / COUNT(resolution_id) BETWEEN 50 AND 75 THEN 'Yellow' ELSE 'Red' END AS success_category FROM resolutions GROUP BY friend_name; Key SQL Features in Action: 1️⃣ Aggregations: Using COUNT() and SUM() to calculate the total resolutions and those completed. 2️⃣ Conditional Logic: Leveraging CASE statements to evaluate the success percentage. 3️⃣ Window Dressing with ROUND(): Making sure percentages look polished and professional. The Results: Here’s how my friends are doing: Alice: 🟡 Yellow, 50% success. Bob: 🟡 Yellow, 50% success. Charlie: ✅ Green, 100% success. Diana: 🟡 Yellow, 66.67% success. Looks like Charlie is absolutely crushing it this year! 🎉 As for the others... maybe next year? 😅 🤩 If you’re thinking about your New Year’s resolutions for 2024, aim for a Green Success Category – let’s keep that success rate above 75%! 💪 Why This Matters: This challenge highlights how SQL can turn raw data into meaningful insights. Whether you're tracking resolutions, analyzing business KPIs, or just having fun with data, SQL is a superpower worth honing! 💪 What do you think of this query? Feel free to share your thoughts or ways to optimize it! 🙌 #SQL #DataAnalytics #SQLChallenge #AdventCalendarChallenge #NewYearsResolutions #DataIsBeautiful
James Isaac’s Post
More Relevant Posts
-
SQL Post 17: GROUP BY and Aggregation – Summarizing Your Data Hello, SQL champions! 👋 We’ve learned how to filter and retrieve data effectively. Now, let’s take it a step further with GROUP BY and Aggregation, tools that help you summarize data and uncover key insights. 📊 Why GROUP BY Matters The GROUP BY clause groups rows with the same values in specified columns, allowing you to apply aggregation functions like: COUNT() SUM() AVG() MIN() MAX() These functions transform raw data into actionable summaries. How GROUP BY Works Here’s the basic structure: SELECT column_name, AGGREGATE_FUNCTION(column_name) FROM table_name GROUP BY column_name; It groups rows based on a column and applies the specified aggregation function. Examples of GROUP BY in Action 1️⃣ Counting Rows How many employees are in each department? SELECT Department, COUNT(*) AS Employee_Count FROM Employees GROUP BY Department; ✅ This groups employees by department and counts how many are in each. 2️⃣ Summing Values What’s the total revenue for each product? SELECT Product_Name, SUM(Sales_Amount) AS Total_Revenue FROM Sales GROUP BY Product_Name; ✅ Use SUM() to calculate total values for each group. 3️⃣ Average Calculation What’s the average salary in each department? SELECT Department, AVG(Salary) AS Average_Salary FROM Employees GROUP BY Department; ✅ Use AVG() to calculate averages. GROUP BY with Multiple Columns You can group by more than one column: SELECT Department, Job_Title, COUNT(*) AS Employee_Count FROM Employees GROUP BY Department, Job_Title; This groups employees by both department and job title. HAVING – Filtering Aggregated Data The HAVING clause filters aggregated results (just like WHERE filters rows): SELECT Department, COUNT(*) AS Employee_Count FROM Employees GROUP BY Department HAVING COUNT(*) > 10; ✅ This shows only departments with more than 10 employees. Challenge for You 💡 Suppose you have a Sales table with columns Region, Product, and Revenue. 👉 Write a query to find the total revenue for each product in the ‘North’ region where the revenue exceeds $10,000. Share your query in the comments! What’s Next? In the next post, we’ll explore Subqueries—queries within queries! Learn how to make your SQL even more powerful and dynamic. Let’s Keep Growing! ✅ Solve the challenge and share your solution in the comments. ✅ Tag a fellow SQL learner to join the conversation. ✅ Follow me for the next post on Subqueries and more SQL insights. Together, we’re turning raw data into stories. Let’s make it happen! 🌟 #SQL #DataAnalytics #DataScience #SQLForBeginners #LearningTogether
To view or add a comment, sign in
-
-
🚀 Day 15: SQL – Comparison Operators in SQL As SQL enthusiasts, we know that mastering the basics of comparison operators can significantly enhance our ability to manipulate and analyze data. Today, I want to share some insights on Comparison Operators in SQL—a crucial part of any SQL query. 🔍 What Are Comparison Operators? In SQL, comparison operators are used to compare two values, helping us filter data based on certain conditions. They enable us to retrieve the information we need by making comparisons between columns and constants. Some commonly used operators include: = (Equal to) <> or != (Not equal to) > (Greater than) < (Less than) >= (Greater than or equal to) <= (Less than or equal to) 🔑 Personal Experience: A Game-Changer in Data Filtering When I first started using SQL, comparison operators became my go-to tool for filtering data with precision. I remember when I needed to extract specific customer data from a large sales database. I used the BETWEEN operator to narrow down sales within a specific range, and it felt like a superpower! 🌟 Using LIKE for pattern matching also came in handy when I needed to find customers with similar email domains, for example. Little did I know at the time, these simple tools would become a foundation for building complex queries. 💡 Industry Insight: Why Comparison Operators Matter In the world of data analysis, SQL is a core skill. Every day, businesses rely on data-driven insights to make decisions. Understanding how to use comparison operators effectively allows us to slice and dice data efficiently, making us valuable assets to any team. It’s not just about pulling data—it’s about retrieving meaningful, relevant information at the right time. 💼 Pro Tip: When using operators like IN or BETWEEN, always double-check your data types! Mismatches can lead to unexpected results. 🤓 📹 Check out this video for a deeper dive: https://lnkd.in/daV6GdF9 So, if you're diving deeper into SQL or just brushing up your skills, get comfortable with comparison operators—they will save you time and help you extract precisely what you need. 🎯 Your Turn: How do you use comparison operators in your day-to-day SQL queries? Share your experiences or tips in the comments below! 👇 #SQL #DataAnalysis #DatabaseManagement #SQLTips #TechSkills #DataDriven #LearningSQL #ComparisonOperators #TechCommunity #SQLForBeginners
To view or add a comment, sign in
-
-
DAY 5: Mastering Data Relationships in Your Database After exploring the core functionalities of SQL, it’s time to delve deeper into one of the most powerful aspects of SQL: Joins. Joins are crucial for querying data across multiple tables, allowing you to harness the full potential of relational databases. What are SQL Joins? SQL Joins enable you to combine rows from two or more tables based on a related column between them. Understanding the different types of joins is essential for efficiently retrieving and analyzing data. Today we'd learn about INNER JOIN LEFT JOIN, RIGHT JOIN and FULL OUTER JOIN with usage examples. 1. INNER JOIN: Finding Common Data The `INNER JOIN` selects records that have matching values in both tables. It's the most commonly used join. ```sql SELECT Employees.FirstName, Departments.DepartmentName FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; ``` This query retrieves employees' first names and their corresponding department names. 2. LEFT JOIN: Including All Records from the Left Table The `LEFT JOIN` (or LEFT OUTER JOIN) returns all records from the left table, and the matched records from the right table. Unmatched records will have NULL values for columns from the right table. ```sql SELECT Employees.FirstName, Departments.DepartmentName FROM Employees LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; ``` This query lists all employees, even those without an assigned department. 3. RIGHT JOIN: Including All Records from the Right Table The `RIGHT JOIN` (or RIGHT OUTER JOIN) returns all records from the right table, and the matched records from the left table. Unmatched records will have NULL values for columns from the left table. ```sql SELECT Employees.FirstName, Departments.DepartmentName FROM Employees RIGHT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; ``` This query lists all departments, even those without any employees. 4. FULL OUTER JOIN: Combining Left and Right Joins The `FULL OUTER JOIN` returns all records when there is a match in either left or right table. Records without a match in one of the tables will have NULL values. ```sql SELECT Employees.FirstName, Departments.DepartmentName FROM Employees FULL OUTER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; ``` This query retrieves all employees and all departments, with NULLs where there is no match. #DatabaseEngineering #DataAnalytics #DatabaseManagement #SQL
To view or add a comment, sign in
-
-
🚀 My Personal SQL Queries Tier List 🚀 As I continue my journey into data analytics, I wanted to share my personal tier list of SQL queries, ranked by how frequently I use them and the value they bring to my work. Here’s my take: 🏆 𝗦–𝗧𝗶𝗲𝗿: 𝗘𝘀𝘀𝗲𝗻𝘁𝗶𝗮𝗹 𝗮𝗻𝗱 𝗜𝗻𝗱𝗶𝘀𝗽𝗲𝗻𝘀𝗮𝗯𝗹𝗲 These are the bread and butter of SQL. This “core set” is foundational for any data retrieval task and is crucial for anyone starting in SQL. Here’s why they’re vital: - 𝗝𝗢𝗜𝗡: Combines data from multiple tables. - 𝗪𝗛𝗘𝗥𝗘: Focuses on relevant information. - 𝗚𝗥𝗢𝗨𝗣 𝗕𝗬 + 𝗛𝗔𝗩𝗜𝗡𝗚: Aggregates data to perform analyses like sums, averages, and counts. - 𝗢𝗥𝗗𝗘𝗥 𝗕𝗬: Sorts data to make it more comprehensible. - 𝗟𝗜𝗠𝗜𝗧: Limits time for data download. 🔥 𝗔-𝗧𝗶𝗲𝗿: 𝗙𝗿𝗲𝗾𝘂𝗲𝗻𝘁𝗹𝘆 𝗨𝘀𝗲𝗱, 𝗛𝗶𝗴𝗵 𝗜𝗺𝗽𝗮𝗰𝘁 As you move beyond the basics, these commands are essential for more precise data manipulation: - 𝗦𝗨𝗕𝗤𝗨𝗘𝗥𝗜𝗘𝗦: Allow for complex filtering and aggregation. - 𝗖𝗔𝗦𝗘: Creates conditional logic within a single query. - 𝗖𝗔𝗦𝗧: Converts data types, crucial when dealing with mixed data formats. - 𝗗𝗜𝗦𝗧𝗜𝗡𝗖𝗧: Removes duplicates, important for data cleaning. - 𝗧𝗢_𝗖𝗛𝗔𝗥 + 𝗧𝗢_𝗗𝗔𝗧𝗘: Formats dates and numbers into readable strings. ⚡ 𝗕-𝗧𝗶𝗲𝗿: 𝗨𝘀𝗲𝗳𝘂𝗹, 𝗯𝘂𝘁 𝗦𝗶𝘁𝘂𝗮𝘁𝗶𝗼𝗻𝗮𝗹 These commands are handy for specific tasks, though they’re not always in regular use: - 𝗨𝗡𝗜𝗢𝗡 + 𝗨𝗡𝗜𝗢𝗡 𝗔𝗟𝗟: Merge results from multiple queries, though I use them less than JOIN. - 𝗧𝗢𝗣 + 𝗢𝗙𝗙𝗦𝗘𝗧: Great for creating data snapshots but not always necessary. - 𝗥𝗢𝗨𝗡𝗗 + 𝗖𝗘𝗜𝗟 + 𝗙𝗟𝗢𝗢𝗥: Control numeric precision. Handy, but reliant on the context. 🌟 𝗖-𝗧𝗶𝗲𝗿: 𝗥𝗮𝗿𝗲𝗹𝘆 𝗨𝘀𝗲𝗱, 𝗕𝘂𝘁 𝗪𝗼𝗿𝘁𝗵 𝗞𝗻𝗼𝘄𝗶𝗻𝗴 While these aren’t part of my daily toolkit, understanding them deepens my appreciation for SQL’s power: - 𝗔𝗟𝗧𝗘𝗥 𝗧𝗔𝗕𝗟𝗘: Modifies table structures, essential for database management but less frequent in daily queries. - 𝗥𝗢𝗟𝗟𝗨𝗣 + 𝗖𝗨𝗕𝗘: Ideal for creating summary reports with subtotals, used in specific scenarios. - 𝗣𝗜𝗩𝗢𝗧 + 𝗨𝗡𝗣𝗜𝗩𝗢𝗧: Transform data, especially in data warehousing contexts, but not something I need regularly. P.S. I didn't mention window functions here as I'm still practicing, but I think they'll eventually find a spot in the A-tier🧐 #Analytics #CareerTransition #DataAnalytics #DataAnalysis #DataCleaning #DataEngineering #DataManipulation #DataRetrieval #LearningJourney #Programming #SQL #SQLQueries #SQLTips #TechCommunity #TechTalk #DatabaseManagement
To view or add a comment, sign in
-
-
Hi Connections, 🎯 Level Up Your SQL Skills with These Questions! 🚀 SQL is the backbone of data management and analysis, making it an essential skill for data professionals. Whether you're starting your journey or sharpening your expertise, here’s a categorized list of SQL questions to challenge yourself and grow! 💡 🔹 𝐁𝐚𝐬𝐢𝐜 𝐒𝐐𝐋 𝐂𝐨𝐧𝐜𝐞𝐩𝐭𝐬: 1️⃣ What is SQL, and why is it important in data analytics? 2️⃣ Explain the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. 3️⃣ What is the difference between WHERE and HAVING clauses? 4️⃣ How do you use GROUP BY and HAVING in a query? 5️⃣ Write a query to find duplicate records in a table. 6️⃣ How do you retrieve unique values from a table using SQL? 7️⃣ Explain the use of aggregate functions like COUNT(), SUM(), AVG(), MIN(), and MAX(). 8️⃣ What is the purpose of the DISTINCT keyword in SQL? 🔹 𝐈𝐧𝐭𝐞𝐫𝐦𝐞𝐝𝐢𝐚𝐭𝐞 𝐒𝐐𝐋: 1️⃣ Write a query to find the second-highest salary from an employee table. 2️⃣ What are subqueries, and how do you use them? 3️⃣ What is a Common Table Expression (CTE)? Give an example of when to use it. 4️⃣ Explain window functions like ROW_NUMBER(), RANK(), and DENSE_RANK(). 5️⃣ How do you combine results of two queries using UNION and UNION ALL? 6️⃣ What are indexes in SQL, and how do they improve query performance? 7️⃣ Write a query to calculate the total sales for each month using GROUP BY. 🔹 𝐀𝐝𝐯𝐚𝐧𝐜𝐞𝐝 𝐒𝐐𝐋: 1️⃣ How do you optimize a slow-running SQL query? 2️⃣ What are views in SQL, and when would you use them? 3️⃣ What is the difference between a stored procedure and a function in SQL? 4️⃣ Explain the difference between TRUNCATE, DELETE, and DROP commands. 5️⃣ What are windowing functions, and how are they used in analytics? 6️⃣ How do you use PARTITION BY and ORDER BY in window functions? 7️⃣ How do you handle NULL values in SQL, and what functions help with that (e.g., COALESCE, ISNULL)? Which of these questions do you find most interesting? Share your answers or thoughts in the comments! Let’s learn and grow together! 🤝 📢 Don’t forget to like, share, and tag your peers who are SQL enthusiasts! #SQL #DataAnalytics #DataEngineering #SQLQueries #LearningSQL #DataSkills
To view or add a comment, sign in
-
100 SQL Commands to Boost Your Data Skills! 🔥 SQL is a cornerstone for data analysis, enabling professionals to retrieve, manipulate, and analyze data efficiently. Here’s a roundup of 100 SQL commands for every aspiring data professional, from basics to advanced skills! 🚀📊 🟢 Basic Commands SELECT – Retrieve data. WHERE – Filter data by condition. DISTINCT – Return unique values. ORDER BY – Sort results. LIMIT – Limit rows returned. OFFSET – Skip rows. AND / OR – Combine conditions. IN – Match any value in a list. BETWEEN – Select range of values. LIKE – Search for a pattern. 🔵 Aggregation Commands 11. COUNT() – Count rows. 12. SUM() – Sum values. 13. AVG() – Average values. 14. MIN() – Minimum value. 15. MAX() – Maximum value. 16. GROUP BY – Group results. 17. HAVING – Filter grouped results. 18. COUNT(DISTINCT) – Count unique values. 19. ROUND() – Round numbers. 20. RANK() – Rank items within a group. 🟠 Data Manipulation Commands 21. INSERT INTO – Add new data. 22. UPDATE – Modify existing data. 23. DELETE – Remove data. 24. VALUES – Define values for INSERT. 25. SET – Specify columns for UPDATE. 26. MERGE – Combine INSERT/UPDATE. 27. UPSERT – Insert or update if exists. 28. REPLACE – Insert or replace row. 29. TRUNCATE – Delete all rows. 30. BULK INSERT – Insert large data. 🟣 Joins – Combining Tables 31. INNER JOIN – Match in both tables. 32. LEFT JOIN – All rows from left. 33. RIGHT JOIN – All rows from right. 34. FULL JOIN – All rows, both tables. 35. CROSS JOIN – Cartesian product. 36. SELF JOIN – Join table with itself. 37. USING – Specify common column. 38. NATURAL JOIN – Join on same column names. 39. JOIN ON – Specify join conditions. 40. UNION – Combine query results. 🟡 Subqueries – Nested Queries 41. IN (Subquery) – Filter by subquery results. 42. EXISTS – Check subquery return. 43. ANY – Compare with any subquery result. 44. ALL – Compare with all subquery results. 45. SOME – Any row meets condition. 46. CORRELATED SUBQUERY – Subquery with main query data. 47. WITH (CTE) – Define Common Table Expression. 48. LATERAL – Access columns outside subquery. 49. FROM (Subquery) – Use subquery as table. 50. NOT IN (Subquery) – Exclude subquery results. 🟤 Advanced Functions 51. CASE WHEN – Conditional results. 52. COALESCE – Return first non-null. 53. IFNULL – Replace nulls. 54. NULLIF – Return null if match. 55. CAST – Convert data type. 56. CONVERT – Data type conversion. 57. LEAD() – Next row value. 58. LAG() – Previous row value. 59. ROW_NUMBER() – Unique row number. 60. NTILE() – Distribute rows into buckets. #sql #DataSkills #DataAnalysis #interview #SQLCommands #TechSkills #preparation
To view or add a comment, sign in
-
SQL commands
GET @Coforge | Writes to 38K | Cybersecurity Enthusiast🔒| AI Content Creator 🎯 Open for Collaboration 🤝
100 SQL Commands to Boost Your Data Skills! 🔥 SQL is a cornerstone for data analysis, enabling professionals to retrieve, manipulate, and analyze data efficiently. Here’s a roundup of 100 SQL commands for every aspiring data professional, from basics to advanced skills! 🚀📊 🟢 Basic Commands SELECT – Retrieve data. WHERE – Filter data by condition. DISTINCT – Return unique values. ORDER BY – Sort results. LIMIT – Limit rows returned. OFFSET – Skip rows. AND / OR – Combine conditions. IN – Match any value in a list. BETWEEN – Select range of values. LIKE – Search for a pattern. 🔵 Aggregation Commands 11. COUNT() – Count rows. 12. SUM() – Sum values. 13. AVG() – Average values. 14. MIN() – Minimum value. 15. MAX() – Maximum value. 16. GROUP BY – Group results. 17. HAVING – Filter grouped results. 18. COUNT(DISTINCT) – Count unique values. 19. ROUND() – Round numbers. 20. RANK() – Rank items within a group. 🟠 Data Manipulation Commands 21. INSERT INTO – Add new data. 22. UPDATE – Modify existing data. 23. DELETE – Remove data. 24. VALUES – Define values for INSERT. 25. SET – Specify columns for UPDATE. 26. MERGE – Combine INSERT/UPDATE. 27. UPSERT – Insert or update if exists. 28. REPLACE – Insert or replace row. 29. TRUNCATE – Delete all rows. 30. BULK INSERT – Insert large data. 🟣 Joins – Combining Tables 31. INNER JOIN – Match in both tables. 32. LEFT JOIN – All rows from left. 33. RIGHT JOIN – All rows from right. 34. FULL JOIN – All rows, both tables. 35. CROSS JOIN – Cartesian product. 36. SELF JOIN – Join table with itself. 37. USING – Specify common column. 38. NATURAL JOIN – Join on same column names. 39. JOIN ON – Specify join conditions. 40. UNION – Combine query results. 🟡 Subqueries – Nested Queries 41. IN (Subquery) – Filter by subquery results. 42. EXISTS – Check subquery return. 43. ANY – Compare with any subquery result. 44. ALL – Compare with all subquery results. 45. SOME – Any row meets condition. 46. CORRELATED SUBQUERY – Subquery with main query data. 47. WITH (CTE) – Define Common Table Expression. 48. LATERAL – Access columns outside subquery. 49. FROM (Subquery) – Use subquery as table. 50. NOT IN (Subquery) – Exclude subquery results. 🟤 Advanced Functions 51. CASE WHEN – Conditional results. 52. COALESCE – Return first non-null. 53. IFNULL – Replace nulls. 54. NULLIF – Return null if match. 55. CAST – Convert data type. 56. CONVERT – Data type conversion. 57. LEAD() – Next row value. 58. LAG() – Previous row value. 59. ROW_NUMBER() – Unique row number. 60. NTILE() – Distribute rows into buckets. #sql #DataSkills #DataAnalysis #interview #SQLCommands #TechSkills #preparation
To view or add a comment, sign in
-
🔥 100 SQL Commands to Boost Your Data Skills! 🔥 SQL is a cornerstone for data analysis, enabling professionals to retrieve, manipulate, and analyze data efficiently. Here’s a roundup of 100 SQL commands for every aspiring data professional, from basics to advanced skills! 🚀📊 🟢 Basic Commands SELECT – Retrieve data. WHERE – Filter data by condition. DISTINCT – Return unique values. ORDER BY – Sort results. LIMIT – Limit rows returned. OFFSET – Skip rows. AND / OR – Combine conditions. IN – Match any value in a list. BETWEEN – Select range of values. LIKE – Search for a pattern. 🔵 Aggregation Commands 11. COUNT() – Count rows. 12. SUM() – Sum values. 13. AVG() – Average values. 14. MIN() – Minimum value. 15. MAX() – Maximum value. 16. GROUP BY – Group results. 17. HAVING – Filter grouped results. 18. COUNT(DISTINCT) – Count unique values. 19. ROUND() – Round numbers. 20. RANK() – Rank items within a group. 🟠 Data Manipulation Commands 21. INSERT INTO – Add new data. 22. UPDATE – Modify existing data. 23. DELETE – Remove data. 24. VALUES – Define values for INSERT. 25. SET – Specify columns for UPDATE. 26. MERGE – Combine INSERT/UPDATE. 27. UPSERT – Insert or update if exists. 28. REPLACE – Insert or replace row. 29. TRUNCATE – Delete all rows. 30. BULK INSERT – Insert large data. 🟣 Joins – Combining Tables 31. INNER JOIN – Match in both tables. 32. LEFT JOIN – All rows from left. 33. RIGHT JOIN – All rows from right. 34. FULL JOIN – All rows, both tables. 35. CROSS JOIN – Cartesian product. 36. SELF JOIN – Join table with itself. 37. USING – Specify common column. 38. NATURAL JOIN – Join on same column names. 39. JOIN ON – Specify join conditions. 40. UNION – Combine query results. 🟡 Subqueries – Nested Queries 41. IN (Subquery) – Filter by subquery results. 42. EXISTS – Check subquery return. 43. ANY – Compare with any subquery result. 44. ALL – Compare with all subquery results. 45. SOME – Any row meets condition. 46. CORRELATED SUBQUERY – Subquery with main query data. 47. WITH (CTE) – Define Common Table Expression. 48. LATERAL – Access columns outside subquery. 49. FROM (Subquery) – Use subquery as table. 50. NOT IN (Subquery) – Exclude subquery results. 🟤 Advanced Functions 51. CASE WHEN – Conditional results. 52. COALESCE – Return first non-null. 53. IFNULL – Replace nulls. 54. NULLIF – Return null if match. 55. CAST – Convert data type. 56. CONVERT – Data type conversion. 57. LEAD() – Next row value. 58. LAG() – Previous row value. 59. ROW_NUMBER() – Unique row number. 60. NTILE() – Distribute rows into buckets. #SQL #DataSkills #DataAnalysis #SQLCommands #TechSkills FOLLOW FOR MORE ABOUT TECHNICAL KNOWLEDGE. Aarti .
To view or add a comment, sign in
-
🔢 Demystifying SQL Window Functions: Unlocking Advanced Data Analysis 🔢 Hey data lovers! Today, let's dive into the powerful world of SQL window functions and explore how they can elevate your data analysis to new heights. 🌐 1. What are Window Functions? • SQL window functions perform calculations across a set of table rows related to the current row. Unlike aggregate functions, they do not collapse rows into a single result, allowing you to retain individual row details while adding valuable insights. 2. Common Window Functions: • ROW_NUMBER(): Assigns a unique sequential integer to rows within a partition of a result set. • RANK() and DENSE_RANK(): Assign ranks to rows within a partition, with ties receiving the same rank. DENSE_RANK does not skip ranks after ties. • NTILE(n): Distributes rows into a specified number of roughly equal-sized groups. • LEAD() and LAG(): Access data from subsequent or preceding rows, respectively, in the same result set. • CUME_DIST() and PERCENT_RANK(): Calculate the cumulative distribution and relative rank of a row within a partition. 3. Syntax Overview: • Window functions are used with the OVER clause, which defines the window or set of rows the function operates on. Example: SELECT employee_id, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS department_rank FROM employees; 4. Practical Applications: • Performance Analysis: Calculate running totals, moving averages, and cumulative sums. • Comparative Analysis: Compare current rows with previous or next rows within a dataset. • Ranking: Generate rankings and percentiles for competitive analysis or performance metrics. 5. Visualization of Trends: • Window functions make it easy to visualize trends over time, such as sales growth or stock price fluctuations, by calculating metrics like moving averages and cumulative totals. 6. Optimizing Queries: • Use window functions to simplify complex queries and reduce the need for subqueries or self-joins, leading to more readable and efficient SQL statements. 7. Combining with Other SQL Features: • Window functions can be combined with other SQL clauses, such as WHERE, GROUP BY, and HAVING, to perform advanced data manipulations and analyses. By mastering SQL window functions, you'll unlock advanced analytical capabilities and gain deeper insights from your data. Ready to elevate your SQL skills? Let's explore these powerful functions together! 🚀 #sqlwindowfunctions #dataanalysis #datainsights #sqlskills #dataengineering #datavisualization #advancedanalytics #sqlqueries #datascience #dataanalytics
To view or add a comment, sign in