SQL Challenge 9/100 : Best Time to Trade Stock 📈 (Part-4) 💪 Difficulty - Hard 🔗 Part 3: Refer old post You are given a table with stock prices 📊 🎯 Problem Let’s make it tougher 👇 👉 You can make at most 2 transactions 🔁 👉 You cannot hold multiple stocks at the same time 🚫📦 👉 Must sell before buying again 🔄 ❓ Your Task Write a SQL query to: ✅ Return maximum profit 💰 ✅ Also return the buy & sell days for both transactions\ ⚠️ Rules: Max 2 transactions only 🔢 No overlapping 🚫 Buy < Sell always ⏳ No hardcoding ❌ -- Create table CREATE TABLE stock_prices ( day INT, price INT ); -- Insert data INSERT INTO stock_prices (day, price) VALUES (1, 100), (2, 180), (3, 260), (4, 310), (5, 40), (6, 535), (7, 695); Tagging Ankit Bansal Sumit Mittal for better reach Sanjay Gatti #SQL #DataEngineering #SQLChallenge #Analytics #LearnSQL 🚀
Sanjay Gatti’s Post
More Relevant Posts
-
This weekend, I’ll be revisiting one SQL topic that can be a little confusing at first but is very important to understand: JOINS In simple terms, “joins” help us combine data from different tables so we can get more meaningful insights. The common types I’m revising are: INNER JOIN – returns only the matching records from both tables LEFT JOIN – returns all records from the left table and the matching ones from the right RIGHT JOIN – returns all records from the right table and the matching ones from the left FULL JOIN – returns all matching and non-matching records from both tables CROSS JOIN – returns every possible combination of rows from both tables One thing I’m learning is that understanding joins is not just about memorising definitions. It’s about knowing when to use each one and what kind of result you want from your data. So this weekend is for more revision, more practice, and more clarity - one query at a time🤗 Which SQL concept are you currently revising or trying to understand better? #SQL #DataAnalytics #DataAnalysis #Omolabakethedataanalyst
To view or add a comment, sign in
-
-
🚀 INNER JOIN in SQL If you're learning SQL, this is one of the most important concepts you must master. 🔍 INNER JOIN 👉 Returns only the rows that have matching values in both tables. 🔑 Key Points: 🔹 Matches rows based on a common column 🔹 Excludes non-matching records 🔹 Works like the intersection of two tables 🔹 Can join multiple tables together 🔹 Combines related data from multiple tables 💡 Example: 👉 The customer table has customer details. 👉 The orders table has order information. 🔹 But not all customers have orders 🔹 Not all orders have matching customers. 💻INNER JOIN Result Only records where Customer_id exists in both tables. Query: SELECT c.Customer_id, c.First_Name, o.Order_id, o.Amount FROM Customers c INNER JOIN Orders o ON c.Customer_id = o.Customer; Tips: INNER JOIN =Only matching data from both tables. #SQL #DataScience #DataEngineer #LearningSQL #TechTips #InterviewPrep #DataAnalytics
To view or add a comment, sign in
-
-
📊 SQL Learning Journey Today, I learned a new concept in SQL: HAVING clause So far, I’ve covered: ✔️ SELECT & filtering using WHERE (AND / OR, LIKE) ✔️ ORDER BY for sorting ✔️ GROUP BY for grouping data ✔️ INNER JOIN for combining tables 🔍 Today’s focus: HAVING At first, it felt a bit confusing, but here’s the simple way I understood it: 👉 WHERE filters rows 👉 HAVING filters grouped data 💡 Example: If I group users by plan, HAVING helps me filter only those plans where users are more than a certain number. This small concept made a big difference in understanding how to analyze grouped data more effectively. Step by step, getting more comfortable with SQL. 📈 #SQL #DataAnalytics #LearningJourney #SQLPractice
To view or add a comment, sign in
-
-
🌳 SQL Practice Series | Problem # 1 Binary Search Tree Node Classification I've been doing consistent SQL practice lately as part of my data analytics journey, and this one made me think. The challenge: given a BST table with nodes (N) and their parents (P), classify every node as Root, Inner, or Leaf using pure SQL. The logic breaks down simply: → Root: P is NULL no one is above it → Leaf: no other node lists it as a parent → Inner: has both a parent and children My Solution: NOT EXISTS with a correlated subquery checks whether any row references the current node as a parent. If none do it's a Leaf. Clean, readable, and it works on any size tree. How would you solve this differently? Would you go with a JOIN, a subquery, or something else entirely? Drop your approach in the comments I'd love to see different ways to think about it! #SQL#SQLPractice#DataAnalytics#HackerRank#LeetCode#DataEngineering#TechInterview#100DaysOfCode#MTSU
To view or add a comment, sign in
-
-
SQL window functions changed how I think about data. Before I learned them, I was writing subqueries for everything. Clunky. Repetitive. Hard to read. Then I discovered window functions, and the same logic became cleaner, faster, and easier for anyone to follow. The one I kept reaching for: ROW_NUMBER() It assigns a unique rank to each row within a group. Simple idea. Powerful in practice. Real example: find the most recent order per customer. Without window functions: → Write a subquery to get max date per customer → Join it back to the original table → Hope nothing breaks With ROW_NUMBER(): → Partition by customer → Order by date descending → Filter where row = 1 Same result. Half the code. Much easier to explain to a colleague. I used this constantly when building SQL pipelines, pulling the latest record per entity from multi-source business data. It saved time and made my queries reviewable. If you're writing SQL regularly and haven't touched window functions yet, ROW_NUMBER() is where I'd start. Small function. Big shift in how you think. Which SQL concept clicked everything into place for you? Drop it below 👇 #SQL #DataAnalytics #DataScience #LearningInPublic
To view or add a comment, sign in
-
-
If you're still relying heavily on subqueries and GROUP BY for everything in SQL, it might be time to level up. Window functions in SQL Server allow you to perform powerful calculations like rankings, running totals, and trend analysis-while still retaining row-level data. This article walks through practical examples using functions like ROW_NUMBER(), RANK(), and more, making it easier to understand and apply in real-world scenarios. 📖 A must-read for anyone working with data. 👉 https://lnkd.in/gQmFTK5t #SQLServer #DataAnalytics #DataEngineering #Developers #LearnSQL #SQL #DataScience #TechSkills #Analytics #Database
To view or add a comment, sign in
-
-
SQL JOIN'S 📌 While learning SQL, one thing became clear to me — data is usually not stored in one place. It’s divided into multiple tables, and that’s where JOIN becomes important. So, what is SQL JOIN? SQL JOIN is used to combine data from different tables using a common column, so we can see the complete picture. Simple example: We have: Customers table (Customer_ID, Name) Orders table (Customer_ID, Order_Amount) Using JOIN, we can connect both tables and understand: which customer made which purchase Types of JOIN: INNER JOIN– gives only matching data from both tables, LEFT JOIN– gives all data from left table + matching from right, RIGHT JOIN – gives all data from right table + matching from left, FULL JOIN– gives all data from both tables, What I understood: JOIN is not just about writing queries… it’s about connecting data to understand what’s actually happening. #SQL #DataAnalytics #LearningJourney #BusinessAnalytics #DataScience
To view or add a comment, sign in
-
-
📊 Strengthening My SQL Skills – Exploring Different JOIN Types After working with INNER JOIN, I continued my SQL learning by exploring other important JOIN types. 🔍 What I practiced: ✔️ Understanding how LEFT JOIN includes all records from the left table, even when there is no match ✔️ Learning how RIGHT JOIN works to include all records from the right table ✔️ Comparing outputs to see how different JOINs affect the final result 💡 Practical Insight: These JOINs are useful for identifying missing or unmatched data, such as: • Customers without orders • Products with no sales • Gaps in data relationships 🚀 Key Takeaway: Selecting the right JOIN type helps ensure more accurate analysis and better understanding of the data. Continuing to build strong fundamentals in SQL and data analysis. #SQL #DataAnalytics #LearningJourney #Joins #BusinessAnalysis #CareerGrowth
To view or add a comment, sign in
-
Day 18 of my SQL learning journey has been insightful as I explored the CASE Statement. I applied IF-THEN logic in SQL, categorized data using conditions, and created dynamic columns. A key takeaway from today is the importance of transforming data using logic, not just queries. SQL continues to grow in power every day. #SQL #LearningJourney #Day18 #DataAnalytics #TechSkills
To view or add a comment, sign in
-
-
🚀 Day 13/30 – Subqueries in SQL Ever felt your SQL queries are getting messy? 🤯 👉 That’s where subqueries come in. 💡 Think of it like this: Solve a small problem first → use that result to solve the bigger one. 🔥 What I learned today: ✔ Subquery runs inside the main query ✔ Helps in dynamic filtering ✔ Makes complex logic simple & clean 🧠 3 Types you must know: 🔹 Scalar → single value 🔹 Nested → multiple values 🔹 Correlated → runs for each row ⚡ Real insight: If you understand subqueries well, you’ll write SQL like a pro analyst 💻 📌 Consistency > Perfection Day by day, getting better 🚀 #SQL #DataAnalytics #LearnSQL #LinkedInLearning
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