ad
ad

Learn Basic SQL in 15 Minutes | Business Intelligence For Beginners | SQL Tutorial For Beginners 1/3

Education


Introduction

Welcome to Vitamin BI, where we bring you business intelligence for beginners and beyond. In this tutorial, you’ll learn the essential SQL querying skills in just 15 minutes. By the end, you'll understand the fundamental aspects of SQL querying and how they can empower your data analysis capabilities.

Why Learn SQL?

You might be wondering why SQL is important, especially with sophisticated BI tools that can generate SQL queries automatically. While many BI tools allow you to drag and drop fields to obtain data for charts, graphs, and dashboards, there are scenarios where writing SQL queries becomes necessary. You'll need SQL to pre-aggregate, filter, and select only the relevant data for your specific project needs.

By creating views, you can optimize performance by connecting to only a small subset of data, rather than accessing large databases containing millions of rows. Having SQL querying skills is invaluable, especially when working with expansive datasets.

Getting Started with SQL

In this tutorial, we will be working with a SQLite database that contains data from the FIFA video game by EA Sports. We will primarily use the player table which includes various columns such as player name, birthday, height, and weight.

The SELECT Statement

To retrieve data from the database, we use the SELECT statement. For instance, to return all columns from the player table, we write:

SELECT * FROM player;

This query selects all fields and rows from the player table. SQL is not case-sensitive, but for clarity, we often capitalize keywords.

To select specific fields, instead of using *, you can specify the columns needed. For example:

SELECT player_name, birthday FROM player;

You can also use aliases to rename columns:

SELECT player_name AS name, birthday FROM player;

Using the WHERE Clause

To filter results, we use the WHERE clause along with comparison operators. For example, to select players weighing 190 pounds:

SELECT * FROM player WHERE weight = 190;

You can use operators like <, >, >=, and <=, as well as logical conditions (AND, OR).

For text values, the LIKE operator can be used. For instance:

SELECT * FROM player WHERE player_name LIKE 'Aaron%';

This returns all players with names starting with "Aaron." The percentage % acts as a wildcard.

Sorting Results

To sort the results, employ the ORDER BY clause. By default, it sorts in ascending order:

SELECT * FROM player ORDER BY weight;

You can specify descending order using DESC:

SELECT * FROM player ORDER BY weight DESC;

Joining Tables

SQL allows you to join data from different tables. For demonstration, we'll join the player attributes table to include player names alongside their ratings. This is done using an INNER JOIN:

SELECT pa.player_api_id, pa.date, pa.overall_rating, p.player_name
FROM player_attributes pa
INNER JOIN player p ON pa.player_api_id = p.player_api_id;

You can also create aliases for tables to make your queries cleaner:

SELECT a.player_api_id, b.player_name
FROM player_attributes AS a
INNER JOIN player AS b ON a.player_api_id = b.player_api_id;

Aggregating and Grouping Data

To summarize data, we can utilize aggregation functions like SUM and AVERAGE. The GROUP BY clause is necessary to group results based on specified fields:

SELECT a.player_api_id, b.player_name, SUM(a.overall_rating) AS total_rating
FROM player_attributes AS a
INNER JOIN player AS b ON a.player_api_id = b.player_api_id
GROUP BY a.player_api_id, b.player_name;

To filter grouped data, utilize the HAVING clause:

HAVING total_rating > 85;

Final Thoughts

By learning these basic SQL querying skills, you've laid a great foundation to delve deeper into data manipulation and analysis. There's much more to explore in SQL, but these essentials will help you get started.

If you found value in this tutorial, please like, share, and subscribe for more informative videos on SQL and business intelligence!


Keywords

SQL, querying, SELECT statement, WHERE clause, ORDER BY, JOIN, aggregation, GROUP BY, HAVING, BI (Business Intelligence).


FAQ

1. What is SQL?
SQL (Structured Query Language) is a standardized programming language used to manage and manipulate relational databases.

2. Why is SQL important for Business Intelligence?
SQL is crucial because it allows analysts to retrieve, filter, and manipulate data efficiently, enabling informed decision-making based on data insights.

3. How do I filter data in SQL?
Data can be filtered using the WHERE clause along with various comparison operators like =, >, <, AND, and OR.

4. Can I join tables in SQL?
Yes, you can join tables using commands like INNER JOIN to combine data from two or more tables based on related columns.

5. What is the difference between GROUP BY and HAVING?
GROUP BY is used to arrange identical data into groups, while HAVING is used to filter records after the grouping has been performed, typically with aggregate functions.