In today’s data-driven world, having the ability to access and manipulate data is crucial for making informed decisions. While complex data tasks often require technical expertise, basic SQL (Structured Query Language) queries can empower non-technical stakeholders, such as Business Developers and Product Managers, to fetch rudimentary data and create insightful dashboards using tools like PowerBI. This tutorial aims to provide a gentle introduction to SQL queries, enabling you to harness the power of your data independently.

Introduction to SQL
SQL is the standard language for interacting with relational databases. It allows users to create, read, update, and delete data in a structured and efficient manner. SQL is widely used across various industries and is essential for tasks ranging from simple data retrieval to complex data manipulation. This tutorial will cover basic SQL queries, including SELECT, WHERE, LIKE, and JOIN operations, which are fundamental for data analysis and reporting.
Basic SQL Queries
Understanding Data Structures and Tables
Before diving into the queries, let’s look at the structure of our sample database. We will use two tables: Customers and Orders.
Customers Table
| CustomerID | FirstName | LastName | City | Status | |
|---|---|---|---|---|---|
| 1 | John | Doe | john.doe@mail.com | New York | Active |
| 2 | Jane | Smith | jane.smith@mail.com | Los Angeles | Active |
| 3 | Sam | Johnson | sam.johnson@mail.com | Chicago | Inactive |
Orders Table
| OrderID | OrderDate | CustomerID |
|---|---|---|
| 101 | 2024-06-01 | 1 |
| 102 | 2024-06-02 | 2 |
| 103 | 2024-06-03 | 1 |
| 104 | 2024-07-01 | 3 |
1. Retrieving Data with SELECT
The SELECT statement is the cornerstone of SQL. It is used to fetch data from a database. Here’s a simple example:
SELECT * FROM Customers;
This query selects all columns from the Customers table. However, you can specify particular columns to retrieve:
SELECT FirstName, LastName, Email FROM Customers;
2. Filtering Data with WHERE
The WHERE clause allows you to filter records based on specific conditions. For instance, to find customers from a particular city:
SELECT FirstName, LastName, City FROM Customers WHERE City = 'New York';
You can combine multiple conditions using AND and OR:
SELECT FirstName, LastName, City FROM Customers WHERE City = 'New York' AND Status = 'Active';
SELECT FirstName, LastName, City
FROM Customers
WHERE (City = 'New York' OR City = 'Seattle')
AND Status = 'Active';
This statement retrieves the FirstName, LastName, and City columns from the Customers table where the City is either ‘New York’ or ‘Seattle’ and the Status is ‘Active’.
Here’s the breakdown of the query:
SELECT FirstName, LastName, Cityspecifies the columns to retrieve.FROM Customersspecifies the table to retrieve the data from.WHEREintroduces the condition for filtering the data.(City = 'New York' OR City = 'Seattle')filters the data to include only those rows where theCityis either ‘New York’ or ‘Seattle’.AND Status = 'Active'further filters the data to include only those rows where theStatusis ‘Active’.
So the query will return the first name, last name, and city of customers who are either in New York or Seattle and have an active status.
3. Pattern Matching with LIKE
The LIKE operator is used for pattern matching. It is especially useful when you need to search for a specific pattern within a column. For example, to find customers whose names start with ‘J’:
SELECT FirstName, LastName FROM Customers WHERE FirstName LIKE 'J%';
The % wildcard represents zero or more characters. Similarly, _ represents a single character.
4. Combining Data with JOIN
Often, you need to combine data from multiple tables. The JOIN clause allows you to do this. Here’s an example of an INNER JOIN, which returns records with matching values in both tables:
SELECT Orders.OrderID, Customers.FirstName, Customers.LastName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
This query fetches order details along with the customer names.
5. LEFT JOIN and RIGHT JOIN
While INNER JOIN returns only matching records, LEFT JOIN and RIGHT JOIN include all records from one table and the matching records from the other table. For example, LEFT JOIN returns all records from the left table and matched records from the right table:
SELECT Orders.OrderID, Customers.FirstName, Customers.LastName
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Practical Example: Building a Simple Dashboard
Let’s walk through a practical example. Suppose you want to create a dashboard showing active customers and their recent orders. Here’s how you can do it using the queries we’ve covered:
1. Retrieve active customers:
SELECT CustomerID, FirstName, LastName, City FROM Customers WHERE Status = 'Active';
2. Fetch recent orders for these customers:
SELECT Orders.OrderID, Orders.OrderDate, Orders.CustomerID
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
WHERE Customers.Status = 'Active' AND Orders.OrderDate > '2024-01-01';
3. Combine data for the dashboard:
SELECT Customers.FirstName, Customers.LastName, Orders.OrderID, Orders.OrderDate
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Customers.Status = 'Active' AND Orders.OrderDate > '2024-01-01';