top of page

Introduction to (SQL) Structured Query Language - Part I

Updated: Apr 18

Your Ultimate Guide to Understanding SQL (Structured Query Language) The Basic You Need to Know.

Understanding SQL is crucial for becoming a data analyst. It is a powerful tool that enables analysts to query and manipulate database data, providing valuable insights and answers to essential questions. Using SQL, data analysts can determine the number of customers who purchased in the last month, identify the most popular products, and better understand their data. SQL is an essential skill for data analysts, and mastering it can significantly enhance their ability to work with complex data sets.


Your Ultimate Guide to Understanding SQL (Structured Query Language) The Basic You Need to Know.
Structured Query Language (SQL)

You can click this link below to skip to the topic of your interest.



 

1. Overview of Structured Query Language (SQL)


SQL, which stands for "Structured Query Language (SQL)". This programming language is essential for managing and manipulating relational databases. By mastering SQL, you can take control of your data and unlock a world of possibilities. Don't miss the opportunity to enhance your skills and achieve your goals. Start learning SQL today! which manages data stored in relational databases. It creates, retrieves, updates, and deletes database data. SQL is composed of commands to perform specific tasks, such as creating and modifying tables and querying the stored data. This dynamic language boasts an impressive ability to execute even the most intricate queries while seamlessly manipulating vast amounts of complex data. Its proficiency in managing data is unmatched, Making it an essential tool for streamlining data processing and analysis. It is the most widely used language for accessing and managing data in relational databases.


There are several SQL dialects, or versions, available on the market. Microsoft's Transact-SQL (T-SQL) and Oracle's PL/SQL are the two most popular. Other databases, such as PostgreSQL and MySQL, also have their versions of SQL. When I first studied SQL, I still needed to decide which one to choose from or which book to start with. It is essential to select your preference because you have to stick with this program forever as an analyst dealing with data.


2. Key Concepts and Terminology in SQL


I will give you ten basic examples of terminologies to familiarize you with SQL.


1. Database: A collection of related tables and objects stored in a single file or directory.

2. Table: A related data collection is stored in rows and columns.

3. Column: A single data field in a table, such as a name or address.

4. Row: A single record in a table representing a single item.

5. A primary key is a column or set of columns uniquely identifying each row in a table.

6. A foreign key refers to another table's primary key. This helps establish a relationship between two tables in a relational database.

7. Query: An instruction to the database that retrieves data from a table or multiple tables.

8. SQL Statement: A specific command that can be used to act, such as SELECT, INSERT, UPDATE, or DELETE.

9. Subquery: A query within a query retrieves data from a table or multiple tables.

10. Join: A query that combines data from two or more tables.


3. Writing Basic SQL Queries


Is an example better than a word, right? I will give you some examples of Basic SQL Queries as the following list:


3.1 Example of a SELECT query:


SELECT * 
FROM Products 
WHERE Price> 100

Result = This query will select all columns from the Products table where the Price exceeds 100.


3.2 Example of an INSERT query:


INSERT INTO Products (ProductName, Price, CategoryID) 
VALUES ('Chair', 150, 2)

Result = This query will insert a new row into the Products table with the values 'Chair', 150, and 2 for the ProductName, Price, and CategoryID columns, respectively.


3.3 Example of an UPDATE query:


UPDATE Products 
SET Price = 200 
WHERE ProductName = 'Chair'

Result = This query will update the product price with the name 'Chair' to 200.


For further practice, you can find more information about writing basic SQL queries on websites such as W3Schools, Tutorials Point, and GeeksforGeeks. These websites provide tutorials and examples that can help you learn the basics of SQL. Many books, such as Sam Teach Yourself SQL in 10 Minutes and Learning SQL—or YouTube, are available.


4. Understanding Data Types and Joins


4.1 What is data type?


A data type is a classification of data that defines the data type a column can store. Common data types include text, numbers, dates and times, and binary data. When creating a table, a data type must be specified for each column. Ensuring that only accurate and legitimate data is entered into the database is crucial.


I will show you some examples of the data type, including:


- An integer is a whole number, meaning it is not a fraction or a decimal and has no fractional or decimal parts. Examples of integers include 1, 2, and 3.

- Float: A number with a decimal point of 1.5, 2.7, or 3.9.

- Text: A string of characters such as "Hello" or "Goodbye".

- Date: A date such as 01-01-2020 or 12-31-2020.

- Time: A time such as 09:00:00 or 18:00:00.

- Boolean: A true/false value such as 1 or 0.


4.2 Why is data type so important?


Using appropriate data types ensures that only correct and accurate information is entered into the database. This emphasizes the importance of paying attention to data types when entering information into the database to save time and effort, reduce the possibility of errors, and avoid potential issues in the future. For example, if a column is defined as an integer data type, only whole numbers can be stored in that column. This prevents errors, such as entering a string of characters into a column that expects a number. Data types also help to ensure that the data is stored efficiently, as different data types require different amounts of storage.


4.3 What is joins?


Joins combine data from tables and retrieve related information, such as a customer's order history or products in a category. They can also integrate data from multiple tables and filter the results, such as determining which customers have purchased products in a particular price range.


4.4 Why do joins is essential?


Joins are vital because they allow us to retrieve data from related tables. With joins, finding information such as a customer's order history or products in a particular category would be easier. Joins also allow us to combine data from multiple tables and filter the results, making it easier to answer complex questions or gain insights from the data.


Example of a join:


SELECT Customers.Name, Orders.OrderDate 
FROM Customers 
INNER JOIN Orders 
ON Customers.CustomerID = Orders.CustomerID

Result = This query will select the Name and OrderDate from the Customers and Orders tables, joining the tables on the CustomerID column.


More syntax of joins include:


- LEFT JOIN: Retrieves all rows from the left table, even if there is no match in the right table.

- RIGHT JOIN: Retrieves all rows from the right table, even if there is no match in the left table.

- FULL OUTER JOIN: Retrieves all rows from both tables, even if there is no match in either table.

-When using the CROSS JOIN, every row from the left table is combined with every row of the right table.


I will expand this special section if you want to learn more about joins. When you get used to using joins, you will eventually have questions related to them, such as, What tables do I need to join? What columns should I join? What type of join should I use? What filters should I use to refine the results? How can I optimize the query for better performance? Etc.

Hence, I will explain another time. But for today, remember that Joins are used to retrieve data from related tables, such as retrieving a customer's order history or finding out which products are in a specific category. They can also combine data from multiple tables and filter the results, such as determining which customers have purchased products in a particular price range. Joins are also used to perform calculations across multiple tables, such as determining the total sales for a specific product.


5. Working with Complex Queries and Sub-queries

Working with complex queries and subqueries involves writing SQL statements to retrieve data from multiple tables and filter the results. Complex queries can involve joining various tables, using multiple WHERE clauses, and using subqueries. Subqueries are SQL statements embedded within another SQL statement and are used to retrieve data from one or more tables.


5.1 Example of a complex query:


SELECT Customers.Name, Orders.OrderDate 
FROM Customers 
INNER JOIN Orders 
ON Customers.CustomerID = Orders.CustomerID 
WHERE Orders.OrderDate BETWEEN '2020-01-01' AND '2020-12-31'
AND Orders.Price > 100

Result = This query will select the Name and OrderDate from the Customers and Orders tables, joining the tables on the CustomerID column. The query will only return orders with a date between 2020-01-01 and 2020-12-31 and a price greater than 100.


5.2 Example of a subquery:


SELECT Customers.Name, (SELECT COUNT(*) FROM Orders WHERE Orders.CustomerID = Customers.CustomerID) AS OrderCount 
FROM Customers 

Result = This query will select the Name from the Customers table and the count of orders from the Orders table, using a sub-query to retrieve the count.


5.3 When do I have to use this query?


This query type can retrieve data from multiple tables and filter results. For example, you can retrieve the customer's Name and order history or determine which customers have purchased products in a specific price range.


Extra Bonus!


More examples of complex queries and sub-queries include:


- Retrieving the total number of orders for each customer.

- Find the average Price of products in a particular category.

- Retrieving the most recent order for each customer.

- Finding the customers with the highest total order value.

- Retrieving the number of orders placed in the last month.

- Finding the customers who have purchased a specific product.

- Retrieving the most popular product in each category.

- Finding the customers who have not placed an order in the last year.


If you want me to investigate this topic further, please let me know. You know me. I'm here to help.


Conclusion


Today, we covered five critical Structured Query Language (SQL) topics. These topics include an overview of SQL, key concepts and terminology, writing basic SQL queries, understanding data types and joins, and working with complex queries and subqueries. In the next session, we will discuss creating, modifying, and deleting tables, followed by inserting, updating, and deleting data. We will also cover how to work with views, stored procedures, and triggers, indexing data and optimizing queries, and best practices for writing efficient SQL queries. Please let me know in the comments below if you want to learn more about SQL.


*** This advice applies to all types of SQL, including Microsoft SQL.***

Comentarios


bottom of page