Skip to main content

Part II: Retrieving Data with SQL



 Retrieving data is the heart of SQL. In Part II of our beginner-friendly tutorial series, we’ll dive into the four essential techniques that let you extract, filter, summarize, and refine datasets:

  • Basic SELECT Queries

  • Advanced Filtering and Expressions

  • Aggregation and Grouping

  • Subqueries and Derived Tables

Mastering these topics will empower you to answer real-world questions, from listing customer orders to calculating monthly sales trends.

Basic SELECT Queries

The SELECT statement is your gateway to any relational database. You’ll learn how to:

  • Specify columns and use aliases (SELECT first_name AS fname)

  • Retrieve all fields with SELECT * for quick previews

  • Limit result sets (LIMIT 10, TOP 5) to speed up testing

  • Sort data with ORDER BY (ascending/descending)

Example:

sql
SELECT id, first_name, last_name
FROM customers
ORDER BY last_name ASC;

This simple query fetches a clean, ordered list of customer names in seconds.

Advanced Filtering and Expressions

Once you can pull rows, you’ll want to narrow them down. Advanced filtering covers:

  • Logical operators: AND, OR, NOT

  • Comparison operators: =, <, >, BETWEEN, IN

  • Pattern matching: LIKE '%@gmail.com'

  • Null checks: IS NULL, COALESCE

  • Calculated fields: SELECT price * quantity AS total_cost

Example:

sql
SELECT *
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-06-30'
  AND status = 'shipped'
  AND customer_email LIKE '%@example.com';

This filters orders placed in H1 2024 by customers and shows only shipped items.

Aggregation and Grouping

To transform rows into insights, use aggregation functions:

  • COUNT(), SUM(), AVG(), MIN(), MAX()

  • Group data by one or more columns with GROUP BY

  • Filter aggregated results using HAVING

Example:

sql
SELECT product_category, SUM(quantity) AS total_sold
FROM sales
GROUP BY product_category
HAVING SUM(quantity) > 1000;

Here, you get categories that sold over 1,000 units—perfect for identifying top performers.

Subqueries and Derived Tables

Complex analyses often require breaking queries into steps. Subqueries let you:

  • Nest queries inside SELECT, FROM, or WHERE clauses

  • Use EXISTS / NOT EXISTS to test record presence

  • Build inline views (derived tables) for cleaner logic

Example:

sql
SELECT e.employee_id, e.name, d.avg_salary
FROM employees e
JOIN (
  SELECT department_id, AVG(salary) AS avg_salary
  FROM employees
  GROUP BY department_id
) d
  ON e.department_id = d.department_id
WHERE e.salary > d.avg_salary;

This finds employees earning above their department’s average—insightful for performance reviews or compensation planning.

By the end of Part II, you’ll confidently write queries that retrieve precise datasets, apply rich filters, summarize key metrics, and structure multi-stage analyses. Stay tuned for Part III, where we explore joining tables and combining disparate data sources into unified insights.

Comments

Popular posts from this blog

Alfred Marshall – The Father of Modern Microeconomics

  Welcome back to the blog! Today we explore the life and legacy of Alfred Marshall (1842–1924) , the British economist who laid the foundations of modern microeconomics . His landmark book, Principles of Economics (1890), introduced core concepts like supply and demand , elasticity , and market equilibrium — ideas that continue to shape how we understand economics today. Who Was Alfred Marshall? Alfred Marshall was a professor at the University of Cambridge and a key figure in the development of neoclassical economics . He believed economics should be rigorous, mathematical, and practical , focusing on real-world issues like prices, wages, and consumer behavior. Marshall also emphasized that economics is ultimately about improving human well-being. Key Contributions 1. Supply and Demand Analysis Marshall was the first to clearly present supply and demand as intersecting curves on a graph. He showed how prices are determined by both what consumers are willing to pay (dem...

Unlocking South America's Data Potential: Trends, Challenges, and Strategic Opportunities for 2025

  Introduction South America is entering a pivotal phase in its digital and economic transformation. With countries like Brazil, Mexico, and Argentina investing heavily in data infrastructure, analytics, and digital governance, the region presents both challenges and opportunities for professionals working in Business Intelligence (BI), Data Analysis, and IT Project Management. This post explores the key data trends shaping South America in 2025, backed by insights from the World Bank, OECD, and Statista. It’s designed for analysts, project managers, and decision-makers who want to understand the region’s evolving landscape and how to position themselves for impact. 1. Economic Outlook: A Region in Transition According to the World Bank’s Global Economic Prospects 2025 , Latin America is expected to experience slower growth compared to global averages, with GDP expansion constrained by trade tensions and policy uncertainty. Brazil and Mexico remain the largest economies, with proj...

Kickstart Your SQL Journey with Our Step-by-Step Tutorial Series

  Welcome to Data Analyst BI! If you’ve ever felt overwhelmed by rows, columns, and cryptic error messages when trying to write your first SQL query, you’re in the right place. Today we’re launching a comprehensive SQL tutorial series crafted specifically for beginners. Whether you’re just starting your data career, pivoting from another field, or simply curious about how analysts slice and dice data, these lessons will guide you from day zero to confident query builder. In each installment, you’ll find clear explanations, annotated examples, and hands-on exercises. By the end of this series, you’ll be able to: Write efficient SQL queries to retrieve and transform data Combine multiple tables to uncover relationships Insert, update, and delete records safely Design robust database schemas with keys and indexes Optimize performance for large datasets Ready to master SQL in a structured, step-by-step way? Let’s explore the full roadmap ahead. Wh...