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

Adam Finer - Learn BI Online
11 Nov 202017:39

Summary

TLDRThis video tutorial from Vitamin BI offers a fast-paced introduction to SQL querying, focusing on the essentials for beginners in business intelligence. The lesson covers basic concepts such as select statements, filtering with where clauses, and joining data from different tables. It also touches on aggregating and sorting data, providing practical examples using a SQL database with soccer stats from the FIFA video game. By the end, viewers gain a solid foundation and confidence to explore SQL further.

Takeaways

  • πŸ“š SQL is a fundamental skill for business intelligence analysts, enabling efficient data selection and manipulation.
  • πŸ” BI tools can automate some SQL queries for data presentation, but manual SQL is often needed for pre-aggregation, filtering, and creating views.
  • πŸ“Š Views in SQL are useful for efficiency, allowing BI tools to work with a subset of data rather than the entire dataset.
  • πŸ”‘ The SELECT statement is the basis for retrieving data, using a '*' to select all columns or specifying column names for particular data.
  • πŸ› οΈ Aliases can be used to rename fields and tables for clarity, making queries more readable and understandable.
  • πŸ”„ The WHERE clause with various operators (=, >, <, BETWEEN, IN, LIKE) helps filter specific rows based on conditions.
  • πŸ”€ Text values in SQL can be matched exactly or with patterns using the LIKE operator and wildcards (%, _).
  • πŸ“ˆ The ORDER BY clause sorts results, with default ascending order or descending order specified by DESC.
  • πŸ”— Joining data from different tables is achieved through JOIN clauses, combining related data across tables based on common fields.
  • πŸ“Š Aggregation is done using functions like SUM or AVG, combined with the GROUP BY clause to group data for analysis.
  • 🚫 The HAVING clause filters aggregated results, applied after the GROUP BY function and only to numeric values.

Q & A

  • What is the main focus of the video?

    -The main focus of the video is to teach the basics of SQL querying, specifically on querying data from tables without creating or deleting databases, tables, or data.

  • Why is it important to learn SQL even with the availability of sophisticated BI tools?

    -Learning SQL is important because BI tools may not always be sufficient for pre-aggregating, filtering, and selecting only the necessary data for a project. SQL is useful for creating views, which are more efficient than connecting to all data when only a subset is needed.

  • What does the video use as an example database?

    -The video uses a SQL light database containing data compiled from the FIFA video game by EA Sports as an example.

  • What is the tool used to query the database in the video?

    -The tool used to query the database in the video is called Navicat, a premium version that allows connection to and querying of various relational databases.

  • How does the video demonstrate the use of SELECT statements?

    -The video demonstrates the use of SELECT statements by showing how to retrieve all data from a table using a star (*) and how to retrieve specific fields by listing field names separated by commas.

  • What is the purpose of the WHERE clause in SQL queries?

    -The WHERE clause is used to filter the results based on specific conditions, allowing the selection of only those rows that meet the criteria set by the user.

  • How can text values be searched in SQL queries?

    -Text values can be searched using the equals operator for exact matches, the LIKE operator with wildcards such as percent (%) for pattern matching, and the IN operator for specifying multiple values.

  • What is the role of the ORDER BY clause?

    -The ORDER BY clause is used to sort the results of a query. By default, it sorts the data in ascending order, but the DESC keyword can be added to sort the data in descending order.

  • How can data from different tables be combined in SQL?

    -Data from different tables can be combined using JOIN clauses, such as INNER JOIN, to bring together data from multiple tables based on related fields.

  • What is the purpose of the GROUP BY clause?

    -The GROUP BY clause is used to aggregate data, allowing the combination of multiple rows into a single row by a specific set of criteria, which is often used with aggregate functions like SUM or AVG.

  • How can the results of a grouped query be filtered in SQL?

    -The results of a grouped query can be filtered using the HAVING clause, which is applied to the results of the GROUP BY function and is used to filter based on numeric values.

  • What is the main takeaway from the video?

    -The main takeaway from the video is that it provides a solid foundation in SQL querying basics, equipping viewers with the knowledge and confidence to explore more advanced SQL concepts and applications.

Outlines

00:00

πŸ“š Introduction to SQL Querying

The video begins with an introduction to SQL querying, promising to teach the basics in just 15 minutes. The host, from Vitamin BI, emphasizes the importance of understanding SQL for business intelligence analysts, even in the age of sophisticated BI tools. The video will focus on data selection rather than database or table management. The necessity of SQL knowledge is highlighted for creating efficient views for data analysis. The host uses a SQL light database with FIFA video game stats and Navicat as the querying tool, providing a brief overview of the database structure and tables.

05:00

πŸ” Selecting and Filtering Data

This paragraph delves into the specifics of data selection in SQL. It explains how to use the SELECT statement to retrieve data, including how to select entire tables or specific fields. The concept of aliases is introduced for field renaming. The paragraph then covers the WHERE clause, demonstrating how to filter rows based on conditions using operators like equals, greater than, less than, and the AND/OR logic. It also discusses text matching with the LIKE operator and its wildcard capabilities. The section concludes with an exploration of the IN and BETWEEN operators for text and integer fields, respectively.

10:03

πŸ“ˆ Sorting and Joining Data

The host moves on to discussing the ORDER BY clause for sorting query results, explaining both ascending and descending sorts. It then introduces the concept of joining data from different tables, using the player attributes and player tables as examples. The paragraph explains how to specify fields from different tables, the importance of table aliases for clarity, and the use of INNER JOINs based on common fields. The video also touches on data aggregation with the SUM function and the GROUP BY clause, showing how to combine data from multiple rows into a single result.

15:09

🎯 Advanced Filtering and Finalizing Queries

The final paragraph focuses on advanced filtering techniques using the HAVING clause, which is applied after the GROUP BY function to filter numeric results. The host demonstrates how to calculate averages and filter for specific conditions, such as player ratings above a certain threshold. The video concludes with a recap of the SQL concepts covered, emphasizing the foundational knowledge provided and encouraging viewers to explore SQL further. The host signs off, inviting viewers to engage with the content and continue learning.

Mindmap

Keywords

πŸ’‘SQL

SQL, or Structured Query Language, is a domain-specific language used in programming and data management to manage and query relational databases. In the context of the video, SQL is the primary tool for retrieving, manipulating, and analyzing data from a database, and the video aims to teach the basics of SQL querying to assist in business intelligence analysis.

πŸ’‘Business Intelligence Analyst

A business intelligence analyst is a professional who uses data analysis tools and techniques to interpret data, identify trends, and provide insights that can guide business decision-making. The video is tailored towards individuals considering a career in this field, teaching them the essentials of SQL querying that are crucial for their role.

πŸ’‘Select Statement

A SELECT statement in SQL is used to retrieve data from one or more tables in a database. It specifies the data to be fetched and the conditions that determine which data is selected. In the video, the SELECT statement is the fundamental query used to ask the database to return specific data, either all data with a '*' or specific fields by naming them.

πŸ’‘WHERE Clause

The WHERE clause in SQL is a conditional element that filters the results returned by a SELECT statement, specifying which rows or records should be included in the output based on given criteria. It is essential for narrowing down data to meet specific requirements.

πŸ’‘Join

In SQL, a JOIN operation combines rows from two or more tables based on a related column between them, allowing for the retrieval of combined data that may be spread across multiple tables. This is crucial for integrating data from different sources to create a comprehensive view.

πŸ’‘GROUP BY Clause

The GROUP BY clause in SQL is used in combination with aggregate functions (like SUM, AVG, COUNT, etc.) to group rows that have the same values in specified columns into summary rows. This is essential for data analysis when looking to aggregate data across multiple records.

πŸ’‘ORDER BY Clause

The ORDER BY clause in SQL is used to sort the result set of a SELECT statement in either ascending or descending order based on one or more columns. This helps in organizing the data in a way that is more readable and analyzable.

πŸ’‘HAVING Clause

The HAVING clause in SQL is used with the GROUP BY clause to filter group results based on a condition that applies after the aggregation. Unlike the WHERE clause, HAVING applies to groups of data rather than individual rows.

πŸ’‘Aliases

In SQL, an alias is an alternative name given to a table or a column, which can simplify queries and make them more readable. Aliases are particularly useful when working with multiple tables where column names might conflict or when providing a more descriptive name for a calculated field.

πŸ’‘Navicat

Navicat is a premium database administration tool that allows users to connect to and manage various types of relational databases. It supports a wide range of RDBMS, including MySQL, SQL Server, and more. In the video, Navicat is used as the interface for querying the database and executing SQL commands.

πŸ’‘FIFA Video Game

The FIFA video game series, developed by EA Sports, is a popular football simulation game. In the context of the video, the FIFA video game database is used as an example dataset for teaching SQL querying, providing a relatable and engaging scenario for learning.

Highlights

The video provides a 15-minute crash course on the basics of SQL querying, aiming to demystify the process for beginners.

The focus is on querying data rather than creating or deleting databases, tables, or data.

Learning SQL is essential even with sophisticated BI tools, as they may not always be able to pre-aggregate, filter, or select only the necessary data for a project.

Creating views with SQL is more efficient than connecting to all data when only a subset is needed, speeding up analysis and presentation of results.

The video uses a SQL Lite database with FIFA video game data for practical examples.

Navicat, the tool used for querying the database, is introduced as a premium version that supports various relational databases.

The SELECT statement is fundamental for retrieving data, using a '*' to represent all fields or specific field names.

Field aliases can be created using the 'AS' function for clarity and ease of reference.

The WHERE clause with different operators allows for filtering data based on specific conditions, such as equality, greater than, or between values.

Text values in SQL can be queried using the '=' operator for exact matches or the 'LIKE' operator with wildcards for pattern matching.

The 'IN' operator for text fields and 'BETWEEN' operator for integers provide ways to specify multiple conditions.

NULL values can be filtered using 'IS NULL' and 'IS NOT NULL' clauses.

Sorting results can be achieved with the 'ORDER BY' clause, with options to sort in ascending or descending order.

Joining data from different tables is demonstrated using the INNER JOIN clause to combine relevant data from multiple sources.

Aggregation of data using functions like 'SUM' and 'AVG' can be performed with the GROUP BY clause to group and analyze data effectively.

The 'HAVING' clause is used to filter aggregated results based on numeric values, providing a more refined analysis.

Transcripts

play00:00

- In this video you are gonna learn the basics

play00:02

of SQL querying in just 15 minutes

play00:06

and by the time we are finished

play00:07

you'll be wondering what all the fuss was about.

play00:09

Are you ready?

play00:10

Then let's get started.

play00:13

(upbeat music)

play00:19

Hello and welcome to Vitamin BI,

play00:21

bringing new business intelligence for beginners and beyond.

play00:26

On this channel, I help you do more with data.

play00:28

So if you are new here, consider subscribing.

play00:31

Right, so as I said in my intro,

play00:33

I'm going to teach you the basics

play00:35

of SQL querying the essentials that you'll need to know

play00:39

if you are considering becoming a business

play00:41

intelligence analyst.

play00:43

Obviously we can only go so far in 15 minutes

play00:46

but I promise you that by the time we are finished

play00:49

you'll be able to understand quite a lot

play00:51

of what you might be called upon to do.

play00:54

We'll be focusing specifically on querying data,

play00:57

not creating or deleting databases, tables or data.

play01:01

Just selecting data

play01:03

from tables that we want to return for our specific needs.

play01:07

Before we get started, let me give you a little context.

play01:11

You might be wondering why you need to learn SQL.

play01:14

With BI Tools becoming more and more sophisticated

play01:17

can't they write the SQL queries for me?

play01:20

The answer to that is yes and no.

play01:24

Yes, when it comes

play01:25

to requesting data for different charts, graphs

play01:28

and tables that you might want to present in dashboards.

play01:31

In these cases the BI tool will have some kind

play01:34

of graphical interface to be able to drag

play01:36

and drop fields to return data.

play01:39

But no, in that in a lot

play01:41

of cases you'll need to write SQL queries to

play01:44

pre aggregate filter and select only the data necessary

play01:48

for the project that you are working on.

play01:50

These are often called views

play01:53

and you create views mainly because it's more efficient

play01:56

than connecting to all data when you only need some of it.

play02:01

So you could have a table that contains millions

play02:03

of rows of data, but by creating a view of it

play02:07

your BI tool will only need to work

play02:09

with a few thousand,

play02:11

making analysis and presenting results much faster.

play02:15

So you see, knowing how to query

play02:17

with SQL is a useful skill to have.

play02:20

Let's jump onto my computer and get started.

play02:22

So today we are working

play02:24

with a SQL light database that I found online

play02:27

containing data compiled

play02:29

from the FIFA video game by EA Sports.

play02:32

So soccer stats, I'll leave a link to the database

play02:36

in the description if you want to play around with it too.

play02:39

In terms of the tool I'm using

play02:41

to query the database, it's called Navicat.

play02:44

It's the premium version that allows you to connect to

play02:47

and query pretty much any relational database

play02:50

but they also have a version

play02:51

for specific RDBMS, like MySQL, SQL Server, et cetera.

play02:56

Link also in the description.

play02:58

Here we are in the Navicat interface.

play03:01

On the left we can see that I'm connected

play03:03

to the Soccer DB connection that I've created

play03:06

and in that connection is a database called main.

play03:10

And in that database we have various tables

play03:14

country, league, match player, et cetera.

play03:17

We're going to be working mainly with the player table.

play03:21

If I double click it

play03:22

we'll see the data contained within that table.

play03:25

We've got three different IDs,

play03:28

player name, birthday, height, and wait.

play03:32

What I want to do is write SQL queries and

play03:34

ask the database to return specific data from this table.

play03:38

So I'll open a new query window.

play03:42

Now when we are asking for data

play03:44

we are using what's called a select statement.

play03:47

So that's what we need to write first

play03:50

and if we want to return the whole table

play03:52

and the result we do so with a star.

play03:56

Then we specify the table

play03:58

that we want to select everything from.

play04:00

In this case the table called player.

play04:03

Select star from player.

play04:06

Then we run the query

play04:08

and the database returns all data from that table.

play04:13

You may notice that I've written everything

play04:14

in lowercase because the SQL isn't case sensitive.

play04:18

However, I have the option

play04:20

to what's called beautify the SQL, and when I do

play04:24

you'll notice that select and from have been capitalized.

play04:28

Although it's not obligatory

play04:29

this does actually make your queries easier to read.

play04:33

It's not so important with such a small query like this one

play04:37

but when you've got one with tens, if not hundreds

play04:40

of lines of code, it really does make life easier.

play04:44

So we've selected all fields

play04:45

or columns and all rows from the table

play04:49

but what if we only want to select specific fields?

play04:53

Simple. Instead of the star, we just write

play04:56

out the field names we want separated by commas,

play04:59

like this.

play05:00

Player underscore name comma birthday.

play05:04

I run this and those are the fields returned.

play05:07

When we select specific fields

play05:09

we can also rename them by creating aliases.

play05:12

To do this, we use the as function, so player name as name

play05:19

and we can see that update.

play05:21

We could specify an alias containing a space

play05:24

but to do this

play05:25

we would need to put the name

play05:27

in quotes like this, full name.

play05:32

Let's go back to all data

play05:34

and look now at how we can ask the database to

play05:37

only return specific rows in the result.

play05:40

To do this, we use a wear clause with different operators.

play05:45

For example, we could select only players

play05:48

with a weight of 190 pounds, so we use the equals operator.

play05:54

We could also select any player with a weight greater

play05:57

than 190 pounds or greater than or equal to 190.

play06:02

You see, it's not that complicated and we've gone from

play06:06

over 11,000 records to just under a thousand being returned.

play06:11

We can also specify more than one condition

play06:14

by using either and or or.

play06:17

So weight greater than 190 and height greater than 190.

play06:23

So both conditions must be met.

play06:27

If I change it

play06:27

to or it means that either condition must be met

play06:31

in order for the row to be returned.

play06:34

So that's selecting rows based

play06:36

on the value in integer fields.

play06:39

What about when it comes to text values?

play06:41

Well, we can also use the equals operator

play06:44

to find any row where the text matches exactly

play06:48

with what we specify.

play06:49

Play a name equals in single quotes Aaron Galindo

play06:58

but we could also use the like operator

play07:00

which achieves the same goal.

play07:03

But what if we want to select just rows

play07:06

from all players called Aaron?

play07:08

So basically the player name field starts with Aaron.

play07:12

Here, we can use a percent character after the text to look

play07:16

for like so and we get all Aarons returned.

play07:21

If we wanted to find any player name that ends with Aaron

play07:25

we put the percent character before.

play07:28

Nothing that ends with Aaron.

play07:31

We could put a percent before and after

play07:34

which would basically mean any player name

play07:36

that contains Aaron and we've got Rolando Aarons.

play07:41

Finally, here's an example which puts the percent

play07:44

in the middle of text.

play07:46

So here we'll return all rows where the player name starts

play07:50

with A and ends with N.

play07:55

You can go even further with the like operator

play07:58

by using underscore to represent single characters.

play08:02

An example would be like T underscore M percent.

play08:08

So the underscore is asking

play08:09

for any rose where the player name starts with T,

play08:13

Tten any character between the T and the M

play08:16

followed by anything, and run.

play08:20

We've got Tamas, Tamir, Tim, Timmy, et cetera.

play08:25

There are also things called wild cards that allow you to

play08:28

go even further when specifying the rows you want to return

play08:32

but we won't go into those for this video.

play08:35

We will however, finish looking at the where clause

play08:38

by talking about a couple more operators that you can use.

play08:42

For text fields we have in which allows us

play08:45

to specify multiple or statements more simply.

play08:49

However, it can only be used for exact matches

play08:52

so we can't use percents or underscores with it.

play08:56

So let's look for Ronaldo and Messi.

play08:59

Open brackets, Christiano Ronaldo comma Lionel Messi.

play09:07

For integers we can use the between operator.

play09:10

So I could say where wait between 180 and 190.

play09:16

Finally we have the is null

play09:18

and is not null, null being empty.

play09:22

In this table we don't have any null values,

play09:25

but if I open up the match table

play09:28

we can see that there are lots in there.

play09:30

So select star from match

play09:33

where home player one is

play09:45

or is not null.

play09:51

There we go.

play09:54

Let's go back to our player table

play09:56

and see how we can sort results.

play09:59

We can do this using the order by clause.

play10:03

The rose are currently sorted

play10:04

by the ID field in ascending order,

play10:08

but if we wanted to sort by weight we can say

play10:12

order by weight.

play10:15

This sorts by weight in ascending order

play10:16

because that's the default sorting method

play10:19

when not explicitly specified,

play10:22

but we can change this

play10:23

to descending order by adding DESC.

play10:28

Now we can see who the heaviest player is.

play10:32

When it comes to sorting data using order by,

play10:34

this isn't something that's overly important

play10:37

for our use case

play10:39

because when we are selecting data to create views

play10:42

we are usually then going to connect that view to a BI tool

play10:46

and use that for building individual chart queries

play10:50

and these tools will let you apply sorts to the data.

play10:54

Now we are going to look

play10:55

at how to join data from different tables.

play10:59

To demonstrate this

play11:00

I'm going to use the player attributes table.

play11:04

We can see that in this table we have the player ID

play11:08

but not the player name.

play11:10

To be able to create a view containing the player name

play11:13

plus the overall rating

play11:15

we would need to join the data

play11:16

from the player attributes and player tables.

play11:20

I'm going to start

play11:21

by specifying player APIID comma date comma overall rating.

play11:30

Now I want to get the player name from the player table.

play11:34

How do I do that?

play11:36

Well, because we are going to need fields

play11:38

from two different tables,

play11:40

we need to specify which field comes from which table.

play11:44

We do this by writing the table name, dot field name.

play11:48

So player attributes, dot player APIID.

play11:53

Same for date and overall rating.

play11:57

Now we can specify perhaps

play11:59

after the player ID player dot player name.

play12:04

If I run this, I'll get an error saying that

play12:06

there's no such column as player dot player name

play12:10

because we are saying that we are looking

play12:11

for it in the player attributes table.

play12:15

This is when we need to specify the joining

play12:17

of the tables using one of the four join types.

play12:21

I'm not going to go into them in this video

play12:23

but I'll probably do a separate video that explains them

play12:25

in more detail, so don't forget to subscribe.

play12:29

So in this case we'll use an inner join

play12:32

and we'll say inner join player, the player table

play12:37

on and now we specify what fields we want to

play12:40

use to create the join.

play12:42

In this case, we are going to use the player APIID field

play12:47

which is contained in both tables.

play12:50

And again we have to add the table name first

play12:53

dot field name equals player dot player APIID and run.

play13:01

Now we have the player name appended

play13:04

to the player attributes table data.

play13:06

Pretty clever, right?

play13:08

But let me show you something even more clever.

play13:11

I showed you earlier how to use the

play13:13

as function to create aliases for fields.

play13:16

Well, we can actually do this for tables as well

play13:20

which makes the query much cleaner.

play13:23

First, I'm going to give

play13:24

the player attributes table an alias of A

play13:27

and the player table, an alias of B.

play13:31

I just need to write these two letters

play13:33

after where I've specified each table.

play13:36

So A after player attributes here and B after player here.

play13:43

Once I do this

play13:44

I can now replace the table name for each field like this.

play13:50

You see much cleaner.

play13:53

So you may notice that there are multiple rows

play13:56

for each player on different dates.

play13:59

So how would we add up

play14:01

all of these different values for each player?

play14:04

Well, to do this,

play14:05

we are going to want to aggregate the data

play14:07

using the sum aggregator.

play14:10

I'll add this to the overall rating field

play14:13

and run the query.

play14:15

Ah, that's not what we were expecting

play14:18

but that's because we haven't specified

play14:21

in the query how we want to group the data together.

play14:25

This is when we need to use the group by clause.

play14:29

When we use the group by clause,

play14:31

we need to add into it

play14:32

all of the fields that we need to group.

play14:35

So in this case, essentially all fields apart

play14:38

from the overall rating.

play14:40

A dot player PIID comma B player name comma A dot date

play14:49

and run.

play14:52

This is correct, but not exactly what we want.

play14:55

We want to combine all

play14:56

of the ratings for the different dates.

play14:59

So we actually want to remove the date field

play15:01

from the specified fields as well as the group by

play15:08

and run.

play15:10

That's better, but let's clean it up by aliasing

play15:14

the sum overall rating as rating.

play15:19

Let's sort this descending.

play15:21

Order by rating descending.

play15:26

Strange that Ronaldo and Messi aren't in there at the top.

play15:30

Let's try and see why this is.

play15:33

Perhaps there are just more entries

play15:35

for some players when we sum them up.

play15:38

To find this out,

play15:39

we could add a count of B dot player name,

play15:46

and yes in this case, how about using an average?

play15:50

I'll replace some with average, AVG and run.

play15:55

And there we go.

play15:56

That makes more sense.

play15:59

We're going to go one step further and filter this result.

play16:04

To do that, we can use the having clause.

play16:07

The having clause is only applied

play16:09

to the result of the group by function

play16:11

so isn't the same as the wear keyword that's applied before

play16:15

and it's only applied to numeric values.

play16:18

So in this case, I'm going to ask for only ratings above 85.

play16:23

I need to write it directly

play16:25

after the group by and before the order by.

play16:28

So having rating greater than 85 and run.

play16:36

Only 26 records.

play16:39

And there we have our final query.

play16:42

I'll beautify it and this is what it should look like.

play16:45

You can see that all of the SQL keywords are capitalized

play16:50

and you can see that we've come a fairly long way

play16:52

in a very short space of time.

play16:55

We've only covered a very small part of SQL as a whole

play16:58

so there's masses more to learn

play17:00

but what I've shown you today is a really good foundation

play17:04

for giving you the confidence to dive deeper.

play17:07

Did I not say that once we'd finished

play17:09

you'd wonder what all the fuss was about?

play17:11

If you got value outta this video,

play17:13

please do like, share and subscribe

play17:16

for more videos like this one.

play17:18

Why not start with this playlist here?

play17:22

As always, thanks for watching.

play17:24

I've been Adam Finer and until the next time,

play17:26

stay BI curious.

play17:28

(upbeat music)

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
SQL TutorialData AnalysisBusiness IntelligenceBeginner's GuideVitamin BIDatabase QueriesData SelectionQuery OptimizationJoining TablesGrouping Data