SQL : Basics  ๐Ÿ’ก

SQL : Basics ๐Ÿ’ก

ยท

5 min read

By the end of this article you will be able to write basic SQL queries, I'll make sure to attach some very cool Youtube tutorials that you can watch. SQL happens to be one of the very first things i focused on when i decidedd to transition from Software engineering to Data Analysis.

After finishing Alex Freberg's playlist, i became very comfortable writing some complicated queries. We will start with CRUD queries (Create, Read, Update, Delete) ,Group By, Order By and Joins.

well, First of all, what is a database?

A database is a structured collection of data that is organized, stored, and managed electronically. It is designed to efficiently store and retrieve large amounts of information, allowing for easy access, manipulation, and analysis of data. data is organized into tables, which consist of rows and columns. Each row represents a specific record or entry, while each column represents a specific attribute or characteristic of the data. This tabular structure allows for logical organization and efficient querying of data.

Now, In order to " communicate " with this database, we need an interface. The software that provides this interface is knows as Database Management System (DBMS).

A Database Management System is is a software application or a suite of software tools that facilitates the creation, organization, retrieval, manipulation, and administration of data in a database. It provides an interface between the users or applications and the underlying database, allowing efficient management and utilization of data.

Here are some examples of widely used Database Management Systems (DBMS):

  • MySQL: MySQL is an open-source relational DBMS that is known for its speed, reliability, and ease of use. It is widely used in web applications and powers many popular websites, including Facebook, Twitter, and YouTube.

  • Oracle Database: Oracle Database is a robust and feature-rich relational DBMS. It is known for its scalability, security, and advanced capabilities for managing large and complex data sets. Oracle Database is widely used in enterprise-level applications.

  • Microsoft SQL Server: Microsoft SQL Server is a relational DBMS developed by Microsoft. It provides comprehensive data management and business intelligence capabilities and is commonly used in Windows-based environments.

  • PostgreSQL: PostgreSQL is an open-source object-relational DBMS that offers a balance of performance, scalability, and extensibility. It is known for its strong compliance with SQL standards and is favored by developers and organizations for various applications.

Let's begin by understanding how to create a table in a database.

In SQL, a table is a structured collection of data organized into rows and columns. Each row represents a specific record or entry, while each column represents a specific attribute or characteristic of the data. Creating a table involves defining its structure, including the column names and data types.

To create a table, we use the CREATE TABLE statement. Let's consider an example where we want to create a table called "Employees" with columns for employee ID, name, and age.

CREATE TABLE Employees (
EmployeeID INT,
Name VARCHAR(50),
Age INT
);

In the above example, we use the CREATE TABLE statement followed by the table name "Employees." Inside the parentheses, we define the columns of the table. In this case, we have three columns: "EmployeeID" of type INT (integer), "Name" of type VARCHAR(50) (variable-length character string of up to 50 characters), and "Age" of type INT.

Now that we know how to create a table, let's explore some common SQL queries and concepts:

CRUD Queries:

  1. Create (INSERT): This query allows you to add new data to a table in the database. Here's an example:
    INSERT INTO Employees (EmployeeID, Name, Age) VALUES (1, 'John Doe', 30);
  2. Read (SELECT): This query retrieves data from the database based on specified conditions. Here's an example:
    SELECT * FROM Employees;
  3. Update (UPDATE): This query modifies existing data in the database. Here's an example:
    UPDATE Employees
    SET Age = 35
    WHERE EmployeeID = 1;

  4. Delete (DELETE): This query removes data from the database based on specified conditions. Here's an example:
    DELETE FROM Employees WHERE EmployeeID = 1;
    Now we move to some of the most used queries such as :
  5. Group By:
    The GROUP BY clause is used to group rows in a table based on one or more columns. It is often used in combination with aggregate functions (such as SUM, COUNT, AVG) to calculate values based on grouped data. Here's an example:
    SELECT Department, AVG(Salary) AS AverageSalary
    FROM Employees
    GROUP BY Department;

  6. Order By:
    The ORDER BY clause is used to sort the retrieved data in ascending or descending order based on one or more columns. It allows you to control the presentation of data. Here's an example:
    SELECT * FROM Employees
    ORDER BY Age DESC;

  7. Joins:
    Joins are used to combine rows from two or more tables based on related columns between them. Common types of joins include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. Joins are powerful tools that allow us to extract meaningful information from multiple tables. Here's an example of an INNER JOIN:
    SELECT Employees. Name, Departments. DepartmentName
    FROM Employees
    INNER JOIN Departments
    ON Employees.DepartmentID = Departments.DepartmentID;

Learning SQL is an essential skill for anyone working with data, whether you're a data analyst, data scientist, or database administrator. Luckily, there are many online resources available to help you get started.

I highly recommend checking out the following YouTube tutorials for learning SQL:

"MySQL Tutorial for Beginners -Full Course-" by Programming with Mosh
"SQL Tutorial " by Derek Banas
"SQL Full Course for Beginners" by freeCodeCamp.org

These tutorials provide a comprehensive introduction to SQL, covering the basics as well as more advanced concepts. By following along with these tutorials, you'll gain a solid foundation in SQL and be well on your way to writing complex queries.

In conclusion, SQL is a powerful language for managing and manipulating data in relational databases. By understanding the basics of SQL and practicing with real-world examples, you'll be able to write effective queries and extract valuable insights from your data.

Remember, practice is key. The more you practice writing SQL queries and working with databases, the more comfortable and proficient you'll become. So, dive in, explore the world of SQL, and unlock the potential of your data!

Happy querying!

ย