‼️ The JOIN nobody teaches you in tutorials: LATERAL JOIN ‼️ Every SQL tutorial teaches you INNER JOIN and LEFT JOIN. Nobody talks about LATERAL JOIN 🤯 I stumbled across it while trying to solve a problem that was turning my subqueries into a mess. Here’s what it does and when you actually need it 👇 ――― 🔍 The problem it solves Imagine a customers table and an interactions table. You want the last 3 interactions per customer — not just the latest one. Most people try something like this 👇 ❌ THE MESSY WAY SELECT c.customer_id, (SELECT event_type FROM interactions i WHERE i.customer_id = c.customer_id ORDER BY created_at DESC LIMIT 1) AS last_event FROM customers c; This works for 1 row. But getting the last 3? You’d need 3 subqueries 😵 It breaks down fast. ――― ⚡ LATERAL JOIN to the rescue ✅ CLEAN APPROACH SELECT c.customer_id, i.event_type, i.created_at FROM customers c JOIN LATERAL ( SELECT event_type, created_at FROM interactions WHERE customer_id = c.customer_id ORDER BY created_at DESC LIMIT 3 ) i ON true; 💡 LATERAL means: For each row in customers, run this subquery using that row’s values. 👉 It’s basically a for-loop inside SQL ――― 🚀 Why this matters at scale In real-world systems, this pattern shows up everywhere: • Last N orders per user 📦 • Top 5 products per category 🛒 • Recent activity per account 📊 👉 LATERAL JOIN handles all of them cleanly No repeated subqueries ❌ No messy self-joins ❌ No Python post-processing ❌ ――― 🧠 Where it works ✔ PostgreSQL ✔ BigQuery (as CROSS JOIN LATERAL) ✔ Redshift ✔ Most modern databases ――― 🔥 Day 2 done. 28 to go. #SQL #LearningInPublic #DataEngineering #SQLTips #TechLearning #CareerGrowth #30DayChallenge #PostgreSQL #AdvancedSQL #DataAnalytics
Soumiya R’s Post
More Relevant Posts
-
SQL Progress: self join, numeric, avg, group by! Today I solved a very interesting challenge on LeetCode: "Average Time of Process per Machine". It was a great exercise for using Joins and Math functions together. What I learned today: 1. Self-Join: I learned how to join the same table with itself (Activity a1, Activity a2). This is the best way to compare two different rows—like a "start" and an "end" timestamp—for the same process. 2. Aggregate Functions: I used AVG() to find the average time and ROUND(..., 3) to keep the results clean and precise as required. 3. PostgreSQL Specifics: I learned that using ::numeric is important for the ROUND function to work correctly in PostgreSQL. 4. Grouping: Used GROUP BY machine_id to make sure the calculation is done for each machine separately. I’ve attached my solution below. Question for the experts: Is there a way to solve this without using a Join? I’d love to hear your thoughts! قليل مستمر خير من كثير منقطع #SQL #DataEngineering #PostgreSQL #LeetCode #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
This week was all about going beyond just “learning SQL”. I didn’t just study PostgreSQL — I actually built with it. Here’s what I worked on: 𝗖𝗼𝗿𝗲 𝗰𝗼𝗻𝗰𝗲𝗽𝘁𝘀 • Joins (INNER, LEFT, RIGHT, FULL) • Indexing & query optimization • Transactions & ACID properties 𝗔𝗱𝘃𝗮𝗻𝗰𝗲𝗱 𝗦𝗤𝗟 • CTEs (including recursive) • Window functions (ROW_NUMBER, RANK, LAG) • CASE, COALESCE, ROLLUP Most importantly — applied learning 𝗜 𝗱𝗲𝘀𝗶𝗴𝗻𝗲𝗱 𝗿𝗲𝗮𝗹-𝘄𝗼𝗿𝗹𝗱 𝗱𝗮𝘁𝗮𝗯𝗮𝘀𝗲 𝘀𝘆𝘀𝘁𝗲𝗺𝘀: • 𝗜𝗻𝘀𝘁𝗮𝗴𝗿𝗮𝗺 𝗧𝗵𝗿𝗶𝗳𝘁 𝗦𝘁𝗼𝗿𝗲 𝗗𝗮𝘁𝗮𝗯𝗮𝘀𝗲 • 𝗙𝗶𝘁𝗻𝗲𝘀𝘀 𝗖𝗼𝗮𝗰𝗵𝗶𝗻𝗴 𝗣𝗹𝗮𝘁𝗳𝗼𝗿𝗺 𝗗𝗮𝘁𝗮𝗯𝗮𝘀𝗲 Worked on: • Table relationships (1-1, 1-M, M-M) • Foreign keys & constraints • Structuring data like real applications Big realization: SQL isn’t just about writing queries — it’s about thinking like a system designer. Still a long way to go, but this week felt like a solid step forward. You can also check the two DB designs in my 𝗚𝗶𝘁𝗛𝘂𝗯 𝗿𝗲𝗽𝗼: https://lnkd.in/gHsgtx4W Would love feedback on my DB designs. Thanks Hitesh Choudhary Piyush Garg Akash Kadlag Jay Kadlag Suraj Kumar Jha Chai Aur Code #SQL #PostgreSQL #DatabaseDesign #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
-
🚀 SQL Learning Journey 🗓️Day 30 🌟 Topic:LeetCode Subqueries Mastery Today marks a special milestone as I dive deeper into advanced SQL concepts using subqueries through real LeetCode problems. These problems really tested my understanding of filtering, ranking, and aggregation logic! Problems Solved: 👉 Employees Whose Manager Left the Company (LeetCode 1978) Focused on identifying employees whose managers are no longer present using subqueries in WHERE clause 👉Department Top Three Salaries (LeetCode 185) Learned how to use ranking functions + subqueries to fetch top 3 salaries per department 👉 Friend Requests II - Who Has the Most Friends (LeetCode 602) Applied aggregation with subqueries to find the most connected user 👉Movie Rating (LeetCode 1341) Combined joins + subqueries + grouping to derive meaningful insights ✨Key Takeaways: →Subqueries can simplify complex filtering logic →Correlated subqueries help solve row-wise dependency problems →Ranking functions (DENSE RANK, RANK) are game-changers →Real-world SQL problems require combining multiple concepts ✨ Note: 30 days of consistency, learning, and growth in SQL! This journey is helping me think more analytically and solve problems efficiently. #SQL #LeetCode #40DaysOfCode #DataAnalytics #CodingJourney #Database #Learning
To view or add a comment, sign in
-
-
Hello everyone, I’m writing a multi-part series on fine-tuning Text-to-SQL models using the JAX ecosystem. Part 1 focuses on the initial setup and groundwork. In this part, I use Grain for data loading and prepare the foundation for fine-tuning. I first wrote about JAX two years ago in a JAX vs NumPy performance comparison, and since then the ecosystem has grown a lot. This series is also inspired by Chris Achard’s course “Build and Train an LLM with JAX”, and I wanted to adapt the idea into a practical Text-to-SQL fine-tuning example. Part 1 is here: https://lnkd.in/eKjbJNyY More parts coming soon. I’ll add my older JAX vs NumPy article in the comments.
To view or add a comment, sign in
-
Still writing SQL in your head while working in PySpark? 👀 This cheat sheet will hit the right node. I’ve mapped the most used SQL operations directly to PySpark DataFrame APIs so you can switch faster and write better code. From basics like SELECT, WHERE, GROUP BY to advanced concepts like window functions, joins, and transformations — it’s all in one place. Save it. Use it. Share it. 𝗖𝗼𝗺𝗽𝗹𝗲𝘁𝗲 𝗗𝗮𝘁𝗮 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗣𝗿𝗲𝗽𝗮𝗿𝗮𝘁𝗶𝗼𝗻 𝗛𝘂𝗯: 👉 𝗨𝗹𝘁𝗶𝗺𝗮𝘁𝗲 𝗣𝘆𝘁𝗵𝗼𝗻 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗠𝗮𝘀𝘁𝗲𝗿𝘆 𝗕𝘂𝗻𝗱𝗹𝗲 https://lnkd.in/gc_7wdYu 👉 𝗣𝘆𝗦𝗽𝗮𝗿𝗸 𝗣𝗼𝘄𝗲𝗿 𝗣𝗮𝗰𝗸 (𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 + 𝗛𝗮𝗻𝗱𝘀-𝗼𝗻 𝗞𝗶𝘁) https://lnkd.in/gefBKgq5 👉 𝗖𝗼𝗺𝗽𝗹𝗲𝘁𝗲 𝗦𝗤𝗟 (𝗪𝗶𝘁𝗵 𝗗𝗪 & 𝗗𝗠) 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗮𝗰𝗸 https://lnkd.in/gABP4VzP 👉 𝗖𝗼𝗺𝗽𝗹𝗲𝘁𝗲 𝗦𝗤𝗟 + 𝗣𝘆𝘁𝗵𝗼𝗻 + 𝗣𝘆𝗦𝗽𝗮𝗿𝗸 𝗕𝘂𝗻𝗱𝗹𝗲 (𝗔𝗹𝗹-𝗶𝗻-𝗢𝗻𝗲) https://lnkd.in/gy-MziZf 🔥 𝗘𝘃𝗲𝗿𝘆𝘁𝗵𝗶𝗻𝗴 𝗮𝘁 𝗢𝗻𝗲 𝗣𝗹𝗮𝗰𝗲 (𝗕𝘂𝗻𝗱𝗹𝗲𝘀 + 𝟭:𝟭 + 𝗖𝗼𝗺𝗺𝘂𝗻𝗶𝘁𝗶𝗲𝘀) 👉 https://lnkd.in/gxAkVqzr #DataEngineering #PySpark #SQL #BigData #Databricks
To view or add a comment, sign in
-
SQL Looked Easy at First. Then Came Joins. The class that almost broke me and the lesson that came out of it. I will be honest with you. There was a moment in my last class where I genuinely considered whether this was for me. SQL started simple enough. Selecting columns, pulling records - manageable. Then the complexity arrived, fast and unannounced. SELECT, FROM - "This is fine." Extracting columns and records. Straightforward. I was feeling confident. WHERE, ORDER BY, GROUP BY, HAVING - "Okay, I am still here." Filtering and sorting data. It was getting tougher but I was keeping up. JOINS and Subqueries - "Wait. What?" Combining tables. Nesting queries inside queries. My brain had to work in ways it had never worked before. "Imagine writing a full query, staring at the screen and being too scared to hit Run."😅 That was me. More than once. And somehow that made me laugh and push through. 💡 What SQL Taught Me The biggest shift was learning to slow down before I type a single line. Understanding what the result should look like before writing the query is everything. Because in SQL, you can run a query, get a result that looks perfectly fine and still be completely wrong. That is the part nobody warns you about. Break the question down. Picture the output. Then query. Stressful? Absolutely. Worth it? Without a doubt. Every tool in this training has pushed me past a wall I did not know I had. SQL just happened to build the tallest one yet. Still standing. Still going. 🚀 Where did SQL start to click for you? You can share below Pushed through with the guidance of Obumneme Udeinya #SQL #DataAnalysis #LearningInPublic #SQLJoins #DataAnalyst #LearningInPublic #GrowthMindset #BeginnersJourney #LMTechHub #Cohort6
To view or add a comment, sign in
-
-
🚀 Building something for the SQL learners out there — I'm starting an SQL Series! Whether you're a complete beginner or someone looking to sharpen your database skills, this series is for YOU. SQL is one of the most in-demand skills in tech today — whether you're in data analytics, backend dev, or just getting started with databases, understanding SQL opens doors. 📚 Here's what we'll be covering: 🔹 SELECT Mastery 🔹 JOINs (INNER, LEFT, RIGHT & FULL) 🔹 GROUP BY & Aggregates 🔹 CASE WHEN 🔹 Subqueries 🔹 CTEs & Recursive Queries 🔹 Window Functions 🔹 String Functions 🔹 DateTime Functions 🔹 NULL Handling 🔹 Views & Materialized Views 🔹 Indexes & Performance 🔹 Constraints 🔹 Transactions & ACID 🔹 JSON in MySQL 🔹 JSON in PostgreSQL 🔹 PostgreSQL vs MySQL vs Aurora 🔔 Follow me. Let's learn together. 💡 Drop a 🙋 in the comments if you're joining the series! #SQL #SQLSeries #Database #DataAnalytics #DataEngineering #LearnSQL #TechCommunity #Programming
To view or add a comment, sign in
-
🚀 Deep Diving into SQL — From Experience to Mastery I’ve worked with SQL before, but recently I decided to deep dive deeper into it. And honestly… SQL is a huge ocean. Instead of getting stuck only in theory, I decided to focus on learning by doing. My approach was simple: 1️⃣ Learn or revise advanced SQL concepts and syntax 2️⃣ Practice real-world SQL problems 3️⃣ Solve challenges on LeetCode and HackerRank 4️⃣ Document everything in GitHub repositories 5️⃣ Build a SQL Cheat Sheet to make revision easier This helped me strengthen concepts like: • Joins & Aggregations • Subqueries & CTEs • Window Functions • String, Date & Numeric Functions • Query logic used in real interview problems 📌 Portfolio & Proof of Work 🌐 SQL Cheat Sheet & Learning Hub A structured reference for SQL concepts from basics to advanced. 🔗 https://lnkd.in/eUE4HXvN 💻 HackerRank SQL Practice Solutions Real SQL challenges focused on analytical queries and joins. 🔗 https://lnkd.in/e4qD-_tn ⚡ LeetCode SQL Problem Solving Practicing interview-level SQL questions to improve query logic. 🔗 https://lnkd.in/eqsSnpWJ ✍️ SQL Learning Roadmap Blog Sharing my SQL Mastery in 15 Days learning system. 🔗 https://lnkd.in/eEex3d9d If you're starting your SQL journey, feel free to use these resources as a guide. 📈 Next step: applying these skills to real-world SQL projects and data analysis case studies. Always open to connecting with people in Data Analytics / Data Engineering who are also learning and building in public. 🤝 #SQL #DataAnalytics #LearningInPublic #HackerRank #LeetCode #DataSkills #AnalyticsJourney
To view or add a comment, sign in
-
#sql #data #AI #datafam #postgres #mysql #SQL Cisco (Hard Level) #DAY68 “Convert the first letter of each word found in content_text to uppercase, while keeping the rest of the letters lowercase. Your output should include the original text in one column and the modified text in another column”. ___________________________________________________ CREATE TABLE user_content (content_id INT PRIMARY KEY,customer_id INT,content_type VARCHAR(50),content_text VARCHAR(255)); INSERT INTO user_content (content_id, customer_id, content_type, content_text) VALUES(1, 2, 'comment', 'hello world! this is a TEST.'),(2, 8, 'comment', 'what a great day'),(3, 4, 'comment', 'WELCOME to the event.'),(4, 2, 'comment', 'e-commerce is booming.'),(5, 6, 'comment', 'Python is fun!!'),(6, 6, 'review', '123 numbers in text.'),(7, 10, 'review', 'special chars: @#$$%^&*()'),(8, 4, 'comment', 'multiple CAPITALS here.'),(9, 6, 'review', 'sentence. and ANOTHER sentence!'),(10, 2, 'post', 'goodBYE!');
To view or add a comment, sign in
-
-
BLOG 13 — Subqueries vs JOINs vs CTEs (When to Use What?) In this blog, I explored one of the most important and commonly asked SQL topics — choosing between Subqueries, JOINs, and CTEs. Topics covered: ✔ What are Subqueries, JOINs, and CTEs ✔ Key differences between them ✔ Performance considerations ✔ When to use each approach ✔ Real-world examples Understanding when not to use subqueries is just as important as knowing how to use them. This blog also covers important interview concepts like: ✔ Correlated subqueries ✔ Performance differences ✔ Readability vs efficiency Read here: https://lnkd.in/gKY4k8zF Grateful to Innomatics Research Labs for providing practical exposure and structured learning. Excited to continue building strong foundations in SQL, Data Analytics, and Data Science. Special thanks to the team for their guidance and support: Co-Founder & CEO – Kalpana Katiki Reddy Regional Head – VAMSI KRISHNA KANAGALA Trainer – Swathi Reddy Thatikonda Abhilash Manikanta Mentors: Gogula Vinay Koduri Srihari Dinesh Bodigadla Rahul Janjirala Program Manager – Raghu Ram Aduri Placement Team: Sigilipelli Yeshwanth Sravani Burma Rishita Bhargavi K Eswarkarthic M SQL | Python | Pandas | Data Analytics | Statistics #SQL #DataAnalytics #Database #LearningJourney #InnomaticsResearchLabs #CareerGrowth #Beginner #Portfolio #100DaysOfLearning
To view or add a comment, sign in
More from this author
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