Skip to content
Get App

GCI World 2026 September Session10 During Lecture

GCI World lecture introducing SQL and databases: why databases matter, relational design, benefits of data consistency, and hands-on SQL queries in DuckDB.

Key Takeaways

  • Databases provide consistency, durability, and organized access that file-based data management cannot reliably offer.
  • Relational tables linked by keys are the core structure for combining and analyzing data such as transactions and customer attributes.
  • SQL is a practical, widely used skill for extracting and preparing data before modeling in data science.
  • Careful database design before data collection prevents duplication, loss, and inconsistency.

What the video covers

  • The lecture introduces SQL and databases as an essential skill for practical data science, noting SQL is widely used in industry, often more than Python.
  • Databases are the usual source of data in data science projects, and SQL is the tool used to extract and prepare that data.
  • Good database design aligns with business objectives, prevents data duplication, and keeps records of changes, unlike error-prone shared Excel or CSV files.
  • Databases underpin industries such as finance (ATM and stock trading), retail (point-of-sale), e-commerce, and reservation systems.
  • Five main benefits of databases are discussed: data consistency, durability and backup, consistent access, and independence of storage, plus systematic handling of changes.
  • Four database types are compared using a university directory example: relational, hierarchical, object-oriented, and network databases.
  • Relational databases store data in linked tables; separate tables are used because they are updated at different rates (e.g., frequent transactions vs. rare new customers).
  • ETL (extract, transform, load) is introduced as a key concept for moving data into usable tables.
  • The hands-on portion uses DuckDB to practice SELECT, UPDATE, ORDER BY, GROUP BY, UNION, and JOIN queries on employee and score tables.
  • Students are encouraged to consult official documentation for SQL grammar, since syntax varies somewhat across SQL dialects.

Answers

Questions about this video

Why is SQL considered an important skill for data scientists?

The lecture explains that SQL is widely used in real-world data analysis, possibly more often than Python, and that many tech companies have over half their employees using it daily. In most projects, the data source is a database, so SQL is the means to extract it.

Why are customer data split across different tables?

Each table is updated at a different rate. Transaction tables change frequently, while customer attribute tables only change when a new customer is added, so separating them makes data easier to manage and analyze.

What tool does the lecture use for the hands-on SQL practice?

The lecture uses DuckDB, and the instructor recommends the official DuckDB documentation for looking up SQL grammar whenever needed.

Full Transcript — Download SRT & Markdown

00:04
Speaker A
Then here we go. Today's lecture is about SQL and databases. Today we'll begin with an introduction to databases, where we'll explore the basics of database design and structure.
00:21
Speaker A
Next, we'll dive into SQL, learning how to use this powerful language to interact with and manage our databases effectively.
00:30
Speaker A
After that, we'll discuss the relationship between databases and data science, understanding how they work together to enable insightful data analysis.
00:41
Speaker A
Finally, we'll wrap up with some SQL exercises to give you hands-on experience in writing and executing SQL queries.
00:50
Speaker A
This roadmap will help build a solid foundation in managing and analyzing data efficiently. We have mainly focused on Python up to this point, covering data analysis, data processing, and the basics of machine learning.
01:07
Speaker A
Now we will study SQL, which works like a programming language to handle data. SQL is widely used in real-world data analysis, potentially even more often than Python.
01:20
Speaker A
Some tech companies have over half of their employees using SQL in their day-to-day tasks, so it is an important skill for practical scenarios.
01:30
Speaker A
Why do we need SQL? A typical data science project includes understanding the business domain, processing data, building a model, and going into development.
01:43
Speaker A
In most cases, the source for that data is a database, and SQL is the tool we use to extract the data from it.
01:52
Speaker A
When you need to pre-process data or build a model, you must first obtain that data, and SQL is often the means to do so.
02:01
Speaker A
As we discussed in week one, it is crucial to utilize data strategically. Data needs to be processed and analyzed in order to be used for decision making.
02:14
Speaker A
Information exists in our world in various forms, but only a small portion of it is transformed and stored as data.
02:22
Speaker A
Then the data is processed to be used for decision making. While prior lectures focused on how to deal with the collected data, today we will look at how data should be collected in the first place.
02:36
Speaker A
Let's say we have some data of customers' transactions stored in table format. As the table on the top shows, each deposit or withdrawal is stored per row.
02:49
Speaker A
However, this table only records the customer's ID. The actual attributes of the customers, such as their gender, occupation, or residential area, are stored in a separate table.
03:04
Speaker A
By using SQL, we will be able to access data that is stored in different tables.
03:10
Speaker A
In this example, we can connect the transaction data with the attributes of each customer, which would be the first step to analyzing patterns, such as the relationship between the customers' jobs and their transaction patterns.
03:24
Speaker A
Why would the data be stored in different tables, you might wonder? This is because each table is updated at a different rate.
03:34
Speaker A
The transaction table would be updated frequently, while the customer attribute table will only be updated if a new customer is added.
03:43
Speaker A
By organizing data into these structured tables, we can efficiently manage the data and prepare for different kinds of analysis for making informed decisions.
03:54
Speaker A
Poorly managed data leads to issues. One key point of using a database is to ensure data is not duplicated or accidentally erased, and to keep a record of who made changes.
04:07
Speaker A
Perhaps you have worked with Excel or CSV files locally, and if multiple people edit the same file, there is a chance someone might delete or overwrite data, even if everyone is being careful.
04:20
Speaker A
That is why designing the database is an important step before starting data collection. Database design is a fundamental component of creating effective databases.
04:33
Speaker A
When we talk about database design, we're referring to the process of defining our data processing and analysis strategies by first aligning with our objectives and business requirements.
04:44
Speaker A
This alignment ensures that the databases we construct are tailored to meet the specific needs of our projects.
04:52
Speaker A
Additionally, we focus on designing the data infrastructure and constructing the databases themselves, which involves planning how data will be stored, accessed, and managed efficiently.
05:04
Speaker A
As we build our databases, there are several key considerations to keep in mind. We need to develop an architecture that can process data efficiently, which means optimizing table designs to prevent data duplication.
05:20
Speaker A
It's also essential to have a strong understanding of domain expertise and to leverage cloud services when appropriate.
05:28
Speaker A
By keeping these factors in mind, we can create robust and scalable databases that support our data science endeavors effectively.
05:36
Speaker A
Understanding what a database is crucial, because databases are the backbone of countless industries and sectors.
05:44
Speaker A
Whether we're looking at financial services, where databases handle ATM transactions and stock trading, or retail, managing purchases through point-of-sale systems, databases ensure that information flows smoothly and securely.
06:00
Speaker A
In the realm of e-commerce, platforms like Amazon use databases to process millions of online shopping transactions every day.
06:09
Speaker A
Additionally, reservation systems for booking flights, trains, and event tickets rely on robust databases to manage availability and customer information efficiently.
06:21
Speaker A
By recognizing the diverse applications of databases, we can understand how important they are in empowering the systems and services we use every day.
06:32
Speaker A
There are five main benefits to using a database. The first is data consistency, which helps avoid duplicates and maintain relationships among tables.
06:44
Speaker A
The second is data durability, which means you have a way to recover or back up if data is lost.
06:51
Speaker A
You also benefit from having a consistent method to access data and a level of separation, or independence, in how the data is stored.
07:00
Speaker A
File-based data management can make it easy to lose track of changes or accidentally delete information.
07:07
Speaker A
Databases allow you to handle these scenarios in a systematic way. One of the major benefits of using databases is the consistency of data.
07:19
Speaker A
When we rely on databases, you can access the information you need without having to worry about where it's stored.
07:26
Speaker A
For example, think about how you can view your grades and class schedule all in one place through your university portal.
07:34
Speaker A
This seamless access is possible because the data is managed centrally, which not only makes it easier to retrieve, but also ensures data integrity.
07:45
Speaker A
By having a single source of truth, we prevent errors and inconsistencies, making sure that the information you work with is always accurate and reliable.
07:56
Speaker A
Let's take a closer look at the different types of databases, using a university's faculty and student directory as an example.
08:04
Speaker A
In a relational database, we organize data into separate tables for students, faculty, and courses, all connected through keys.
08:14
Speaker A
With a hierarchical database, the directory is structured like a tree. The university is the root, branching out to colleges, departments, and then to individual faculty and students.
08:29
Speaker A
This is great for clearly defined parent-child relationships, such as departments within colleges. In an object-oriented database, each faculty member and student is treated as an object with their own attributes and behaviors.
08:46
Speaker A
This allows for more complex interactions, like students enrolling in courses or updating their contact information, similar to how objects work in Python.
08:56
Speaker A
Lastly, a network database offers flexible connections between entities. A student can be linked to multiple courses and faculty members, and vice versa, supporting many-to-many relationships.
09:12
Speaker A
This is especially useful in a university setting, where students often take several courses taught by different professors.
09:20
Speaker A
In today's lecture, we will look at how to manage relational databases. A relational database stores data in tables that relate to each other.
09:30
Speaker A
For instance, you might have a customer master table with one record per customer, plus a purchase history table showing which items each customer bought.
09:40
Speaker A
If we look at a customer with ID one, we might see that they have bought a notebook and a computer, and we can then look them up in the master table to see their name or other attributes.
09:52
Speaker A
If you are accustomed to Python's Pandas, you can picture separate data frames that can be related through common keys.
10:00
Speaker A
That is the general idea of a relational database. Thinking about how to organize data into tables, what columns to use, the estimated volume, and the infrastructure you need for storage is a fundamental step in designing a database.
10:17
Speaker A
A relational database goes beyond the mere presence of tables. You also need to decide how data will be managed.
10:27
Speaker A
The term DBMS refers to software that manages the database's core functions. Well-known examples include Oracle, MySQL, and Microsoft SQL Server.
10:41
Speaker A
Although there can be small differences depending on which DBMS you choose, once you have a grasp of SQL fundamentals, you can generally use any of them.
10:52
Speaker A
Now, let's get into what SQL is. SQL is the language that allows you to extract, query, or manipulate the data stored in your database.
11:04
Speaker A
If you use SQL, you can retrieve data from your tables and easily count or aggregate it, much like you do with pandas in Python.
11:14
Speaker A
Moreover, SQL is much faster than Python, especially when dealing with a large-scale database. As we also discussed at the beginning of the lecture, managing databases is the first step to doing data science.
11:32
Speaker A
When it comes to a database's architecture, you typically do not work directly on the production environment.
11:39
Speaker A
Instead, you create a separate environment for data analysis, often referred to as a data lake or data warehouse, where you conduct model validation or exploration.
11:51
Speaker A
At this point, you decide whether to use Python or SQL or both based on data volume.
11:58
Speaker A
If the data stretches into hundreds of millions of rows, you can get far better response times by querying the database with SQL rather than relying solely on pandas.
12:10
Speaker A
Once you have built a database and started running various systems and services, you will often see an increase in the number of tables and also in how they interconnect.
12:21
Speaker A
It is not uncommon in practical scenarios to have thousands or even tens of thousands of tables.
12:28
Speaker A
This can go beyond what any individual can fully grasp. Various new technologies and services have emerged to consolidate data or help manage these structures.
12:40
Speaker A
The important takeaway is that a great deal of effort goes into data preparation before you do any analysis.
12:48
Speaker A
You also have to plan how the data will be loaded into the database. That process is called ETL, which stands for extract, transform, and load.
13:00
Speaker A
ETL is an important keyword to remember. You extract data from its source, transform it into a usable form, and load it into a table.
13:12
Speaker A
The transformation step can be crucial in real-world projects because you cannot simply use raw data for analysis.
13:20
Speaker A
You need to think about how best to convert the raw data for modeling or analysis.
13:26
Speaker A
One of the major challenges with legacy systems is that, according to Experian, a whopping 89% of companies struggle with them.
13:36
Speaker A
These old systems often make it difficult to connect with recently updated or modified systems, creating barriers when trying to integrate new technologies or tools.
13:47
Speaker A
For example, if a company wants to implement a modern analytics platform, their outdated database might not support the necessary connections, leading to data silos and inefficiencies.
14:00
Speaker A
Additionally, legacy systems typically have complex database and table structures, which can be hard to navigate and maintain.
14:09
Speaker A
This complexity not only slows down data analysis, but also increases the risk of errors, making it harder for us to work effectively on data science projects.
14:22
Speaker A
By understanding these challenges, we can better appreciate the need for modern, flexible database solutions that support our data-driven goals.
14:32
Speaker A
Data usage requires a strategic approach. Sometimes you do ad hoc projects, but for a longer view, you need to consider how the data will be stored and how it will be used.
14:45
Speaker A
If you are going to build your own service at some point, you must think about how best to accumulate data.
14:52
Speaker A
I hope you will continue studying these aspects after this lecture. To summarize what we've covered so far, databases process data efficiently by using optimized table designs that prevent data duplication, ensuring our information remains organized and reliable.
15:10
Speaker A
Relational databases organize data into interconnected two-dimensional tables using keys, which makes managing and retrieving data much simpler.
15:20
Speaker A
With SQL, we can handle large-scale data processing, allowing us to work with vast amounts of information seamlessly.
15:30
Speaker A
As the volume of data continues to grow, it's crucial to store it in relational databases so that we can analyze it effectively using tools like SQL or Python.
15:41
Speaker A
By understanding these fundamentals, we're well equipped to manage and analyze data efficiently in our data science projects.
15:50
Speaker A
Now that we've summarized how databases and SQL help us manage and analyze large volumes of data efficiently, let's put this knowledge into practice with some SQL exercises.
16:03
Speaker A
These hands-on tasks will allow you to write and execute SQL queries, create tables, insert data, and perform various data manipulations.
16:14
Speaker A
Writing SQL is very similar to writing pandas. Generally, you follow this order. Select the columns you want from the table you specify and then use where if you need a condition.
16:30
Speaker A
The table name you specify can be thought of like a pandas data frame. With SQL, you can also write group by if you want to aggregate by a particular column and use functions like sum or count.
16:46
Speaker A
The logic is similar to pandas, but the order you write things is different. Now that we've covered these fundamental SQL operations, let's move on to applying them practically using our notebook.
17:00
Speaker A
This hands-on approach will help solidify your understanding and give you the confidence to manipulate databases effectively.
17:12
Speaker A
So welcome back. Now let's put those ideas into to practice. Oops. Something. Okay. So we'll use Google Colab for the exercise.
17:33
Speaker A
If you've used Colab before, open today's notebook from the materials folder. Choose connect more apps, search for collaboratory and connect it to Google Drive, then open the notebook with Colab.
17:51
Speaker A
If you are still getting set up, you can watch the first example and join us when you are ready.
18:05
Speaker A
Oops something. Okay, let's explore SQL together. Please open the notebook and run the cell in order with that with me.
18:19
Speaker A
For each example, we'll look at the question, read the query, and then check the result. Before running a query, try to predict what it will be, what it will return.
18:33
Speaker A
That will help you connect the SQL to the data. Oh, let me change my screen now.
18:47
Speaker A
Okay, let's get started now. You can see my screen, right? Call up notebook. Okay.
18:55
Speaker A
Chapter overview. What is database? Okay. A database. What is database? It is a collection of data that is managed so it can be utilized for various needs later on by using a computer to consolidate large amounts of information
19:14
Speaker A
in one place. So what is database? Look at this picture here. Oh, I think it's time log.
19:26
Speaker A
Yep. Okay database stands for this cylinder green shape. Then in the database, you can create some tables as much as you want as long as your memory is, you know, allowed.
19:48
Speaker A
Your memory, the amount of your memory allows. And what is a fields? It's a column.
19:56
Speaker A
Like your name, your department, or your address, something like that. And row. Well, here, for example, there are four rows here, and each row is sometimes called record.
20:14
Speaker A
There are four records here in this table one like this. RDBMS. Okay, the database system we'll be studying in this chapter is called RDB MS, relational database management system.
20:42
Speaker A
Major example includes Oracle, Microsoft Microsoft SQL Server, PostgreSQL, SQL, and MySQL. These are four major four major RDBMS.
21:00
Speaker A
So like if you are hunting a job, maybe you want to say like I know one of these, so I can I can do others too because those syntax are very similar but slightly different, unfortunately.
21:16
Speaker A
But compared to programming languages, the difference is really small, not big. And here we see the picture of MySQL Workbench.
21:30
Speaker A
Well, today we are going to use DuckDB, so we cannot use this application named called MySQL Workbench.
21:40
Speaker A
But here if you use MySQL, you can use this application. This is a graphical diagrams like each this rectangle shows a table and attributes, I mean fields, columns name like a customer ID here I can see and the store ID.
22:03
Speaker A
And each table relates to each other, right? Something like this. Yes, and we are going to use DuckDB today. And the official document is here, duckdb.org. So whenever you encounter or you want to know the grammar, I mean,
22:25
Speaker A
syntax, please go to the this link, and check the official documents. Here is our reference. If you are for those who have never used SQL before, here are references.
22:45
Speaker A
And both are free to use. And the first one is Open Stacks. It is the it is run by Rice University in the United States.
22:58
Speaker A
And it's for free and all textbook are reviewed officially. So it's very good one for beginners.
23:06
Speaker A
Then the second one, a practical introduction to databases by Runstone Academy. This is also famous one, especially in the university, I mean, among the universities in the United States.
23:23
Speaker A
Yep. Like uh I think last semester the first computer I mean [clears throat] introduction to database classes in computer science department at the MIT stands for Massachusetts Institute of Technology.
23:43
Speaker A
Use this site, website to study data I mean SQL. So it it this is also a good material.
23:55
Speaker A
Okay, now SQL basics. First, we got it to run this cell. Do you remember the shortcut?
24:05
Speaker A
Click the code this code block and shift enter or just click this this one, this button.
24:16
Speaker A
Now it's connecting, so it takes a little bit time. So let me talk while running that, let me talk about the pronunciation differences.
24:26
Speaker A
Well SQL is pronounced I mean pronounced among non English areas such as Japan or Egypt or whatever who don't speak English mainly.
24:41
Speaker A
We call it as we pronounce it SQL in professional field. However, in the United States they tend to pronounce sequel. Why?
24:52
Speaker A
That's because well SQL I mean database language the first database language in human history was invented by IBM in 1970s.
25:05
Speaker A
Yeah, it's America in this in this in what? America invented the first database language.
25:16
Speaker A
And the name was not it's not like a SQL it was like a five five letters sequel.
25:24
Speaker A
Probably something like S E Q U E L something like that. So they that's why they keep saying sequel.
25:36
Speaker A
And first commercial RDBMS was Oracle. But the second one is Microsoft SQL S SQL Server.
25:47
Speaker A
So still they use SQL. However um PostgreSQL SQL and my my SQL is those are OSS and those are developed by other areas other people in other countries. So they often say SQL my SQL not my sequel something like
26:09
Speaker A
this. Okay, I think I'm ready to set up the dev environment here. Hopefully it's same as you.
26:24
Speaker A
Okay. And now the second code block. Just write. This this kind of connecting duck DB that we installed here.
26:40
Speaker A
Can you see my Oh, maybe you can't see my mouse pointer. Okay. And here it one of setting configuration.
26:54
Speaker A
If you want to use a lot of records in this like Jupiter notebook, you got to do you got to set up this.
27:08
Speaker A
Single magic display limit now. Otherwise you you know you can't see all records in this Jupiter or any other Jupiter. It's a kind of limitation to use Jupiter for SQL.
27:22
Speaker A
But yeah, today we don't need it, but in case I just do that. Okay.
27:29
Speaker A
So percent percent SQL. At the beginning of the cell, every time you want to add the percent SQL, this is a kind of magic word for Jupiter notebooks.
27:48
Speaker A
Only for Jupiter notebook, okay? Okay. Then about the duck DB. Well, it's automatically creates database already and the schema.
28:06
Speaker A
But if you first installed like a MySQL, you want to do like a create a database first before creating a table.
28:15
Speaker A
Uh like this create table, create a database and database name. But here we are using duck DB. We don't have to care about it.
28:25
Speaker A
Now let's create your first table in SQL query. Don't forget about this magic keyword when you whenever you use Jupyter Notebook and shift enter.
28:41
Speaker A
Count. It says count. Usually it shows the number of table you created, but it's a kind of display bug here, unfortunately, in Jupyter always.
28:58
Speaker A
Okay. And do this. Select ampersand um ampersand No asterisk. Asterisk means everything. So select statement do like a from this table emp list select all data.
29:22
Speaker A
But here we do not have any data, I mean records yet, so it's empty. It's just to show the column names, right? Like we created column names ID and the column names name.
29:36
Speaker A
Oh anyway ID is integer. It's a data type. And name's type is varchar. It's a characters.
29:51
Speaker A
Let me talk about primary key a little bit. So if you set ID as a primary primary primary key, you can't duplicate any IDs.
30:05
Speaker A
You can't have the same ID number like ID one ID one ID one No, it's not allowed. It's going to be fault.
30:18
Speaker A
Okay now let's insert some records here. Anyway, this emp list stands for, I believe, employee list.
30:31
Speaker A
So we are going to use employee list table. Okay. Like this. Like this. Like this.
30:41
Speaker A
Just shift enter three times in a row. Then check the table data here. There are three tables.
30:56
Speaker A
And these are [snorts] Japanese last name Yamada san, Tanaka san and Sato san. Sato san Yep.
31:04
Speaker A
[clears throat] San means mister or miss. It's a yeah. We have three records here now.
31:12
Speaker A
It's easy, isn't it? You've done already. It's super easy, right? So far. You don't need to memorize the statement syntax yet.
31:28
Speaker A
Just play with it. Yeah, that's your first step. Just play with it. And here I'd like to insert into the table with the values ID is one Yamada san again.
31:46
Speaker A
It's already here, but okay, let me try. It's Yep. One time error here. Error duplicate key.
32:00
Speaker A
ID one violates primary key constraint uh constraint right? Because ID column is primary key.
32:13
Speaker A
So you can like I said before, we can't duplicate the another ID one. If you said or you change like a ID four, it might be good. It should be good.
32:26
Speaker A
Okay. It's easy, isn't it? Okay, I'm checking that question question from Q and list now. Let me pick up one of them.
32:51
Speaker A
So it's okay, how does SQL differ from NoSQL, Python, MySQL and PostgreSQL and how is it used alongside Python?
33:02
Speaker A
Okay, first NoSQL is not relational database. Uh it's it's data is CVS. In in NoSQL the database itself, data file itself is a CV CSV or JSON looks like JSON. It's not like totally different from SQL.
33:29
Speaker A
NoSQL stands for not only SQL. But SQL stands for relational databases. Okay? And Python is a programming language.
33:43
Speaker A
And MySQL and PostgreSQL is other another other RDM RDT RDB MS. It's a long.
33:59
Speaker A
And though yeah, among those you know they are like syntax is a little slightly different from each other.
34:09
Speaker A
And another question is how is the used alongside Python? So any programming languages have the method to connect to SQL.
34:21
Speaker A
So well, it's a good idea after you try play with SQL then why don't you try to use Python to connect to SQL directly.
34:35
Speaker A
And in Python program, you can state like you can write query like a select statement or create data create tables or something like this, insert, delete, update, whatever, any queries.
34:49
Speaker A
It's a good practice too. Thank you for your question. Okay, let me go back.
34:58
Speaker A
Oh, we don't know we don't have much time. Okay, now so we have runtime error here. Then if you see runtime error, especially runtime error, um please do rollback.
35:11
Speaker A
It's kind of cancel the former execution. So every time you see you might want to do rollback, but the rollback also has another major purpose to to run.
35:30
Speaker A
In my office hour on Sunday in it's coming Sunday is upcoming Sunday. I'm going to talk about the details a little bit about it. Okay.
35:40
Speaker A
Let's go. Now selecting rows. Okay. Please run this code block here. Well yeah, it the how can I say it? Well, it's the list of tables.
36:05
Speaker A
And uh it's yeah, in that DB we use SQLite master table to check table to check table's name.
36:16
Speaker A
But in other SQL it's pretty different. It really depends on your SQL Thank you our question 10 to 10 1 but we do not have much time now, so let me skip them.
36:37
Speaker A
Searching, updating, and deleting data and columns. Yep. So now well, this is a condition.
36:49
Speaker A
From the table named employees, select all data, but condition is ID is two. Only the data whose ID is two, you can see the you can select [clears throat] by using where, okay?
37:10
Speaker A
So we have an ID two, Tanaka san. And another keyword, in. Where ID in one two, what does it show?
37:27
Speaker A
It is like either one or two. Either ID is one or two. Get the I mean retrieve records from employees table.
37:41
Speaker A
So we can get this. Another keyword, okay, like. Let me try that now. Okay, again from that table named employees, select all data and condition is name start with S.
38:05
Speaker A
Is only Sato san, that 's why we get only this one record. Right? Sato san. Here we have only three records here in this table and only Sato san started with S, that's why.
38:22
Speaker A
Okay. Oh. Now let's update to change the record for the person whose ID is three.
38:32
Speaker A
Whose ID is three. And update the name to Suzuki san. Okay. Let me do this.
38:46
Speaker A
So now uh ID three, the name is Sato san, right? Here. Here. However, we are going to change update the ID three the name Suzuki san.
39:04
Speaker A
Okay, let's do select again. See now, Sato san is bye bye. See you Sato san. Now welcome to Suzuki san in number three.
39:16
Speaker A
It's easy right? And insert the dummy. ID four and the name is dummy. Yep, it's in the table right now. Then let's do try delete statement. Delete this statement to is removing data and a record as long as a condition matches. So ID is
39:49
Speaker A
four dummy right now. Deleted from the employee table. Okay. Shift enter. Then oh, see you dummy record now.
40:07
Speaker A
Right? It's very easy. Okay now. [clears throat] Sometimes after you creating the after you create the any tables and you want to change the columns.
40:19
Speaker A
Oops. Right? In that case, we can use alter table. Let's do like add column class and class the data type is integer.
40:38
Speaker A
And I did it and it succeeded. And also add age column column named age and its data type is integer too.
40:54
Speaker A
And check the data now. See? Class and age columns are added in the employee table.
41:05
Speaker A
But still there none. It says none. That means empty. Because we just added the columns, not data records.
41:15
Speaker A
Okay. Now from empty you we want to change the empty empty here into data, real value data. So let's do use let's use update statement now.
41:33
Speaker A
And let me check that. So ID 3 Suzuki san's class and age is 1 and 13.
41:42
Speaker A
It's updated. Succeeded. Let's continue update other records too and check it again. Okay. No empty cells now in this table.
42:00
Speaker A
It's succeeded. Okay, then here um data what insert a record. And now we have a lot of data here by running this.
42:22
Speaker A
Ooh, it's time limited. Okay. It's pretty easy, right? So for later code block we need to use we need to do question 10 5.
42:47
Speaker A
It's mandatory. It will be used in subsequent problem, but for now we skip it and I'm going to add it later just before we need.
42:59
Speaker A
Okay, now this is another important keywords. So sometimes we want to know the number of records.
43:09
Speaker A
For that we can use count parentheses and asterisk asterisk. Okay. [snorts] How many data how many records now in this table?
43:26
Speaker A
Well, it's eight. It's correct. And if we use as count I mean as keyword we can change the column's name only when only when you run this code.
43:47
Speaker A
Okay. We don't change the column's name itself but you know when you retrieving some data only when we use that some data.
44:02
Speaker A
We can change the column's name here. Okay, now again class count three. Okay. New keyword distinct.
44:14
Speaker A
If you distinct count distinct class. So how many kinds of classes in the table right now?
44:24
Speaker A
We have three because oh I see. Here class one two one two three We have only three kinds of classes right here in this table. That's why we get three.
44:45
Speaker A
Where was it? Where was it? Where was it? Yeah, here. Okay. Do this and another one average age of average. We want to know the average of age as of age name.
45:02
Speaker A
Okay, here what and cast with the sum, sum function would give us the total value of columns.
45:20
Speaker A
So because we calculated like this, this is, do you remember? It's the same as Python. It's a division.
45:30
Speaker A
So this is sum of age divided by the number of of age and you get 13.5. Well, you know, well, it's a little bit tricky here, but I'm going I'm going to talk about because we do not have much time now, so I'm going to
45:58
Speaker A
talk about the details in my office hours later on Sunday. Minimum okay. You can use minimum too, the minimum of age in the table and maximum.
46:17
Speaker A
Minimum age is 11, max age is 16. Yep. We can do like this always.
46:29
Speaker A
Oh, we have only 10 minutes left now. Uh okay. So now sorting always remember where order by keyword phrase and column names and descending.
46:58
Speaker A
Or the opposite is ascending, a s c. Let's do this. Descending in by order by ID here 8 7 6 5.
47:13
Speaker A
Right? Something like this. Order by age. Here. The default is ascending in duck DB, I think.
47:33
Speaker A
Here, 11 to 16 through What if we do order by class and ages two columns?
47:51
Speaker A
So this order is important. First, order by is execute round by run in class. Then ages order matters.
48:06
Speaker A
So this the order the order of this columns is important after order by keywords.
48:15
Speaker A
Limiting number of rows to return. Okay. Let's do this. Well, we have eight records in this table, but we want to know only three records.
48:29
Speaker A
Then use limit keyword, limit three. We have only three there. Get. It's like a toy SQL, isn't it? It's easy.
48:44
Speaker A
I hope. So now where key where conditions we have a two. So we wants um record and the condition is class is two and age eight at least 15 so only these two people, two people's records are matched.
49:14
Speaker A
Oh, what about this? Class one or okay or age is at least 15. Yep.
49:24
Speaker A
Only five people I mean five records are here matched. Okay 10.2.4 aggregating by groups. This is important people.
49:42
Speaker A
This is important. Well, first to do this, first to run it then check the result.
49:55
Speaker A
Sometimes it helps. So okay, count now and then please group by classes. So we have three kinds of classes right now, right?
50:09
Speaker A
And for each class, how many records are there in the table? So class one's records are we have three.
50:20
Speaker A
Something like this. Group by aggregating by group. It's also possible to calculate averages for each group. Similarly, you can find minimums and maximums.
50:39
Speaker A
Do this. Well, it's just added the average number. Oh, here, average ages. Average of ages by group, grouped by.
51:00
Speaker A
Grouping by is doing cut kind of categorizing, okay? Here now another keyword filter by conditions. To filter by conditions, we are going to use having.
51:17
Speaker A
It's totally different from group by. Group by is categorizing, but uh filtering by condition is having. It's important.
51:30
Speaker A
So what do does it do? So group by class and having average over 13.
51:44
Speaker A
Average age is over 13. So only class one and two have the average of age is 13.
51:55
Speaker A
That's why we have only two two kinds of you know data here. So from only one table, we can manipulate or calculate with conditions the table, I mean the data, okay? Data in the table.
52:19
Speaker A
Okay, to filter before grouping, you use where. Right? Oh. What will we get? Oh.
52:35
Speaker A
Yep. Something like this, group by. Okay, that's pretty much. These are different in where having or where it's instead, okay.
52:49
Speaker A
This is group by only group by one two directly stated. It's different. Wait, it's same.
53:09
Speaker A
12 12 okay. So first table it's to access for aggregation. We will count the records by class by age. That's all.
53:23
Speaker A
But the second select the statement specify which columns to group by by by specifying their columns and numbers.
53:33
Speaker A
Okay. So group by and having is different. Having is a condition after grouping. Important.
53:50
Speaker A
Okay, using multiple table. I think here for here we need to do extra things. Just a moment please.
54:06
Speaker A
Where is my like a the execution from practice question. From here maybe you cannot run this because you don't have the answer yet.
54:28
Speaker A
It is the answer for practice 10 5. And here create another table and please the two. Okay.
54:48
Speaker A
Oops. Well, uh just a one we do not have much time though. Well, if you are to running this in not collab server, I mean collab but that in your local machine, Well, you want to delete this because always
55:12
Speaker A
you want to start percent percent SQL, okay? But in collab, it's okay. It allows us to do this.
55:25
Speaker A
So first check that employee list is empty now and insert some values. Let me do a little quick.
55:32
Speaker A
So now employee list two have this records. Union operator, so now we have two tables now available, employee list one and employee employee list just employee list or employee list two, okay?
55:47
Speaker A
[clears throat] And union operator to concatenate multiple result of select statements, we can use union keyword.
55:56
Speaker A
Um let's go a little quick. So it's union from two tables. It's just a select all from both table.
56:10
Speaker A
And another union again, union all. You will see two records for ID one. Join operator.
56:30
Speaker A
But let's create another table score and insert some data. And check the new score table.
56:40
Speaker A
So we have now math, English, science scores and one only one is empty now.
56:50
Speaker A
So join operator to join multiple table, there are four types of joins. Maybe you learned already about these diagrams, right?
56:59
Speaker A
In your maybe mid school or junior high I mean junior high school or elementary school even.
57:05
Speaker A
I think you've already studied here like a left join, right join, inner join, and full join.
57:13
Speaker A
We can select the data like a in left join or whatever, any kinds of joins using the keyword join like this.
57:25
Speaker A
So now employee join score score if its IDs are same from both. Then you look like this.
57:38
Speaker A
Concatenate that data from two kinds of tables. It's a left join. Okay? Then do like you get like this.
57:54
Speaker A
This is called self join because from employee join the same employee. That's why it's called the self join.
58:06
Speaker A
Okay. [clears throat] And please exercise your you know these questions here of case expressions. So sometimes like a Python we want Python we want to use the case and when class is one then state A like this.
58:30
Speaker A
Okay. Let's do this. As class name. See? As class name. If class is one then state A, B, or C like this.
58:50
Speaker A
It's pretty useful database language, isn't it? If you feel so it's great. Sub query. So you know what Yeah, sub query in professional fields sub query you is used often so much.
59:09
Speaker A
Like select look at it. After Sorry. After from keyword, you see you don't see the table name itself, but you see another select statement.
59:23
Speaker A
From this the result of this select statement then select the name and age. Yep.
59:36
Speaker A
It's same. Okay. Wow, it's already 9:12 now. View. View is important to create view.
59:50
Speaker A
Oh, yeah. View is not a table itself, but it's a kind of nickname from the result nickname as the result of a select statement okay?
60:05
Speaker A
So the result of this select statement is stores now V1. And the view V1 we can use in the select in another select statement, we can use the V1 like a table after from.
60:27
Speaker A
Yep, it's done. Pretty much another view two. But however, this is big different from difference between table and views.
60:41
Speaker A
So insert into V view one you we we cannot insert any records because it's not table. View is just a view.
60:52
Speaker A
It's different from table. See now SQL Master. So what kind of names I mean what kind of views did we use so far? We created V1 and V view two, right?
61:08
Speaker A
With this statement, it's recorded in in the SQL Master table. This SQL master table is automatically created after creating database.
61:25
Speaker A
Okay, pretty much that's all. Let me change the what to show now.
Topics:SQLdatabasesrelational databaseDuckDBdatabase designdata scienceETLJOINGROUP BYdata consistency

Get More with the SozAI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries and speaker detection. 30 minutes free.

Or transcribe another YouTube video here →