SQL Tutorial for Beginners (and Technical Interview Questions Solved)

freeCodeCamp.org

SQL Tutorial for Beginners (and Technical Interview Questions Solved) by freeCodeCamp.org

This SQL tutorial for beginners covers the basics of relational databases and SQL, including setting up a database, creating tables, and performing CRUD operations. The video also explains the importance of using entity-relationship diagrams to visualize the structure of a database and provides an introduction to SQL syntax and the different types of SQL statements. The instructor demonstrates how to access MySQL on different operating systems, use the MySQL workbench to manage databases, and insert data into tables using SQL commands like "describe" and "select".

00:00:00

In this section, the creators of the SQL tutorial course introduce the basics of relational databases and SQL, including setting up a MySQL database, working with data types and constraints, and performing CRUD operations. They also provide a hypothetical scenario for Classic Models, Inc. to illustrate why a relational database is necessary to manage information about their products, sales representatives, customers, and transactions in a single central place. This course aims to guide beginners and those preparing for technical interviews to gain knowledge and confidence in SQL.

00:05:00

In this section, we learn the importance of managing information in a central location via a relational database. While there are many ways of storing data on a computer, a relational database is a data storage system that stores data in tables, with each table having a set of columns that are used to store a specific type of data, and where the data in the tables is stored as rows or records. Each table supports CRUD operations, and tables can support relationships between each other, one-to-many, or many-to-many, which can be linked with records across tables. Lastly, users can retrieve the data from the database using SQL structured query language, with databases either hosted locally or on the cloud for distributed access.

00:10:00

In this section, the speaker discusses relational databases and their important features, which include storing data in tables, columns having specific types, and using relationships to connect tables. The speaker explains that when setting up a database, an entity relationship diagram (ERD) is commonly created to describe all the tables and their relationships within the database. ERDs are created using drawing tools like Lucidchart and they show each box as a table with a list of columns in the table, including a primary key, which is a unique identifier for that table. The ERD does not contain any actual data, but it is a blueprint for the database.

00:15:00

In this section of the video, the speaker explains the relationships between tables in a relational database and gives examples of tables such as employees, offices, customers, products, and order details. The speaker also mentions the importance of understanding entity-relationship diagrams and how different relational database software packages may have slightly different syntax, but the concepts learned in the tutorial will be applicable across any of them. Finally, the video gives a brief introduction to SQL and its uses for interacting with relational databases.

00:20:00

In this section, the video discusses how SQL is used to interact with relational databases and the specific use cases for SQL. Unlike general-purpose programming languages, SQL has a limited syntax and is specifically designed to work with tables in relational databases. SQL cannot be used for data visualization, building software, web applications, creating scripts, or machine learning. The video goes on to explain the basic structure of a SQL statement and working with databases, such as selecting a subset of rows and columns. The three types of SQL statements are also discussed: data definition language, data control language, and data manipulation language. The most frequently used subset of SQL is the data manipulation language, which covers the CRUD operations: create, read, update, and delete.

00:25:00

In this section, the speaker explains the three ways in which SQL statements are used: to create databases and set up the structure, manage permissions, and perform operations on data. The speaker also notes that SQL syntax is case insensitive and allows for backticks or backcodes around database, column, and table names. Statements can span multiple lines and must be ended with a semicolon. Different relational databases may have slightly different syntax, but the operations covered can be executed on MySQL and accessed through the command line or workbench. The speaker encourages viewers to download and experiment with MySQL locally.

00:30:00

In this section, the instructor explains how to access MySQL on different operating systems. On Mac OS, MySQL can be found in the /usr/local/MySQL/bin folder, where the command line interface can be accessed by running the 'MySQL' command. By default, the username to access MySQL is 'root' and a password can be set during installation. The MySQL server runs automatically on the computer and can be controlled through system preferences. It is also possible to connect to a MySQL database on the cloud by specifying the URL through the command line tool. Additionally, MySQL can be accessed through the MySQL workbench, which requires a separate installation.

00:35:00

In this section, the speaker explains how to download and use MySQL Workbench, a graphical user interface that connects to a MySQL server. They demonstrate how to run queries on the command line and in the Workbench interface, and how to list and create databases. They also caution against disturbing default databases and advise obtaining server details from one's team or supervisor when connecting to cloud-based databases. Finally, they show how to create a new database, in this case named "classic models."

00:40:00

In this section, the video shows how to create and manage databases in MySQL using SQL. The speaker explains that all database names, column names, and table names need to be lowercase and have back ticks for visual separation. Creating a database is done using the CREATE DATABASE statement, while deleting a database is done using DROP DATABASE. To select a database, the USE statement is used. The video also advises caution when using DROP DATABASE since it permanently deletes all data without warning or confirmation. The viewer is encouraged to try creating, deleting, and renaming databases using the covered commands.

00:45:00

In this section, the instructor discusses how to work with tables in SQL. One can list the tables in a database using the "show table" statement after selecting the specific database using the "use" command. To create a table, one can use the "create table" statement where they specify the name of the table, the columns with their data types, and any additional constraints or information about the table, such as the primary key constraint that uniquely identifies each record in the table. A table can only have one primary key constraint. The instructor provides an example of creating a table for the offices based on its entity relationship diagram.

00:50:00

In this section, the video discusses primary keys and selecting multiple columns as the primary key, and explains that a primary key can consist of multiple columns but should only be written once. The video also covers data types in SQL, including numeric data, bits, integers, and decimals, as well as date-related types, character types such as character and text, and specialized types such as blobs. The video notes that not all data types are supported by all SQL software, and suggests using specific data types to optimize performance. Finally, the video explains that once a table is created, you can check it with the show tables command.

00:55:00

In this section of the tutorial, the instructor covers the basics of creating a table in SQL and inserting data into it. They explain how to use the "describe" command to get information about the columns in a table and how to specify default values for certain columns. They also demonstrate how to retrieve data from a table using the "select" command, specifying which columns to select and from which table.

01:00:00

In this section, the tutorial covers inserting and viewing multiple rows of data in a table. Inserting multiple rows uses the same syntax as inserting one row, but with sets of values separated by commas. The tutorial shows an example of copying and pasting seven sets of values to insert into the offices table. To view the data in the table, the select statement is used with the syntax "select column one comma column two comma, so on from table name" or "select star from table name" to view all columns. The tutorial also provides exercises for the viewer to try inserting more entries into the offices table using only required columns.

01:05:00

In this section, the video tutorial covers the importance of testing errors and provides examples of common mistakes made when inputting data into the MySQL table, such as not providing a value for a non-null column or creating a duplicate entry. Additionally, the tutorial demonstrates how to retrieve and display specific data from the table using a select statement. The tutorial moves on to the creation of a second table, "employees," and explains the significance of columns like office code and reports to. The video also emphasizes the importance of dropping previous tables before creating new ones and provides an example of the drop table command.

01:10:00

In this section, the speaker discusses the concept of foreign keys and how they ensure that a column in one table contains a valid value from another table. Specifically, the speaker demonstrates how to insert values into a table and how to ensure that foreign keys are properly enforced. The speaker emphasizes the importance of referencing primary keys when working with foreign keys to ensure data integrity.

01:15:00

In this section, the tutorial covers the concept of foreign key constraint and how it affects the addition of child rows. The video shows an example of a failed foreign key constraint when attempting to add an employee with an invalid office ID to the employee table. The tutorial also includes exercises for viewers to practice inserting entries with invalid office codes or values for reports and creating new office locations. The tutorial then moves on to the creation of the customers table, which has a foreign key reference to the employees table for sales reps. The tutorial explains the different options for creating a table and advises viewers to experiment with them.

01:20:00

In this section of the tutorial, the instructor demonstrates how to add data to a database table using SQL commands. The SQL file contains "insert" statements for the customer table, which he copies and pastes into the MySQL Workbench. He explains that as the number of rows in the table grows, it becomes difficult to browse or visualize all the data at once, and that is where the "where" clause in the select statement becomes useful as it makes it possible to filter out data based on specific conditions such as a job title.

01:25:00

In this section, the video shows how to filter data out from a database table using the WHERE clause in SQL. The WHERE clause supports different operations, such as equal to, less than, greater than, and not equal to, for various data types. Users can also combine expressions in the WHERE clause using AND and OR. Additionally, the video demonstrates how the WHERE clause can be used with SELECT, UPDATE, and DELETE statements. The tutorial concludes by showcasing a delete statement that deletes a row from a table.

01:30:00

In this section, the YouTube video walks through the process of deleting columns using SQL, including how to delete multiple columns. The tutorial also covers update commands and provides an example of how to use them to change the reporting person for certain employees. The video provides several exercises for practice, including listing customers in the United States with credit limits higher than a certain amount and listing employee codes for sales representatives of customers in certain countries.

01:35:00

In this section of the tutorial, the instructor demonstrates how to use SQL queries to list the names and email addresses of sales reps who work with customers in Spain, France, and Italy. The instructor begins by selecting the first name, last name, and email from the employees table, with a condition statement where the employee number is in a list of specific codes. The instructor also shows how to use the "like" statement to search for a list of customers with "toys" in their name. Furthermore, the instructor introduces the product lines and products tables and explains the meaning of the columns present in each table.

01:40:00

In this section, the video tutorial discusses creating the product lines and products tables, which contain information such as the product code, name, vendor description, quantity in stock, buy price, and retail price. To order data and limit results, the SELECT statement can be combined with ORDER BY and LIMIT, and the WHERE clause can be used to filter data, such as selecting customers with a credit limit greater than $100,000 and ordering them by country. The tutorial also poses several exercises for the viewer to practice combining these SQL statements, such as identifying the five most expensive products from the planes product line and listing ten products in the motorcycle category with the lowest buy price and more than 1,000 units in stock.

01:45:00

In this section, the instructor discusses how to modify the structure of a table using SQL's ALTER TABLE statement to add, remove, rename or modify columns. The instructor demonstrates how to add a column to the employees table, and how to insert data into that column. The instructor then suggests a helpful exercise to try adding, removing and modifying one column in all the tables created, and to check what happens when you remove or rename a primary key column that is also a foreign key, and how to place SQL commands into a database dump SQL file to automatically import the data into your SQL database.

01:50:00

In this section of the video, the instructor explains two ways to execute SQL code. The first method is through the command line where the user logs in as the root user and uses the ">" symbol to pass the data in the SQL as inputs to the command. The second method is through using MySQL workbench where the user can open an SQL file and execute the code. The instructor also shows how to export data out using the MySQL dump utility.

01:55:00

In this section, the tutorial covers the MySQL dump utility that allows users to export their databases into a SQL file. The instructor demonstrates how to use the MySQL dump command to create a SQL file containing all the data from a specific database. Users can also choose to include the "drop-table" command, and the tutorial shows how to redirect the MySQL dump output to a file. Additionally, the tutorial discusses how to import SQL files into different formats and converters to CSV files, and challenges the viewer to use their newly acquired skills to complete some exercises from a database management course.

02:00:00

In this section of the video, the instructor discusses various SQL interview questions and how to approach them using basic SQL commands such as select, count, and sum. The questions include finding the number of employees in a company, the total payments received, and the list of product lines that contain cars. The instructor also suggests using resources such as W3Schools and MySQL documentation to practice and improve SQL skills. The next section of the video will cover advanced SQL concepts such as using functions for string and date manipulation, aggregating data using count, average, and sum, and using joins to query data from multiple tables.

02:05:00

In this section of the tutorial, the instructor recommends some resources for learning SQL. He suggests using the W3 schools reference and the slides available for free at dbbook.com for a comprehensive introduction to SQL and relational databases. He also mentions that practicing with exercises is key to mastering SQL, and recommends w3resource.com for additional exercises. In addition, he encourages becoming familiar with the workbench or command line for accessing SQL databases in the real world, although he notes that he will touch on accessing them using Python. Finally, he summarizes what has been covered so far in the tutorial, which includes use cases and design of relational databases, installation and setup of MySQL, data types and constraints, CRUD operations on databases, and exporting/importing data.

02:10:00

In this section of the tutorial, the instructor provides an overview of what will be covered, which includes aggregation, grouping, pagination, mapping functions, arithmetic, working with dates, SQL joins, improving query performance with indexes and views, executing SQL queries using Python and SQL Alchemy, and setting up a MySQL server. He explains that a server is a program that listens for instructions and MySQL server can be downloaded from dev.m.mysql.com. Once installed, the server can be started and MySQL commands can be issued through the MySQL command line tool or the MySQL Workbench. The MySQL Workbench is a graphical user interface that can connect to any database on the cloud, provided the host name, port, username, and password are provided.

02:15:00

In this section of the video, the instructor provides an overview of the Classic Models hypothetical company and the relational database that will be used to track its activities. An entity relationship diagram of the database is presented, detailing the various tables and their relationships. The instructor also explains how to set up the database locally with sample data using a SQL script downloaded from the internet. The section concludes with an invitation for viewers to post any questions they may have about the database or its setup in the Zoom chat.

02:20:00

In this section, the video demonstrates how to set up a database in MySQL workbench using an SQL script. The script not only sets up the database but also inserts sample data into the tables created. Once executed, the user can browse the tables in the schema section of the sidebar in MySQL workbench. The video also discusses aggregation, grouping, and aliases in SQL. Several functions are listed, including count, average, sum, min, and max, which can be used to aggregate the results of a query. An example query is discussed, which involves reporting the total number of payments received before a certain date.

02:25:00

In this section of the video, the instructor explains how to filter results by a specific date range and count the number of rows returned by a query in SQL. He demonstrates how to use the WHERE clause and the COUNT function to filter and count the number of payments received before October 28, 2004. He also answers a question about the source of the payment table and provides an example of counting the number of distinct values in a column, specifically the number of unique customers who made payments before October 28, 2004.

02:30:00

In this section, the instructor explains how to select distinct values from a column in SQL. By using the "distinct" keyword followed by the column name, it filters out repeated values. The instructor also demonstrates how to count the number of unique values in a column and how to use the "distinct" keyword with multiple columns. Finally, the instructor shows how to retrieve details for all the customers who have made a payment before a certain date by using a query that utilizes the results of the previous query.

02:35:00

In this section, the video discusses chaining SQL queries and using the result of one query as another table for writing SQL queries. An exercise is given for retrieving the details of all customers in the United States who have made payments between April 1, 2003, and March 31, 2004. The video then moves on to discuss grouping in SQL queries and provides an example of finding the total number of payments made by each customer before October 28, 2004. The video demonstrates how to achieve this by grouping the results by customer number and using the COUNT function to count the total number of payments made by each customer.

02:40:00

In this section of the tutorial, the instructor demonstrates how to use the GROUP BY clause with aggregation functions like COUNT and SUM to obtain informative data from a database table. The instructor shows how to group data by customer number to obtain the number of payments each customer has made before a specific date. The instructor also shows how to compute the sum of values in a column using the SUM function and how to combine COUNT and SUM functions to get the total number of payments and payment amount for each customer before a specific date. Finally, the instructor provides an exercise for the viewer to determine the total number of units sold for each product in a database table.

02:45:00

In this section, the instructor takes a closer look at the SQL query for finding the total number of units sold for each product by using the "Group by" function and the "Sum" function. The instructor emphasizes the importance of typing out the code to become familiar with SQL. Additionally, the instructor demonstrates how to use the "Min," "Max," and "Average" functions for basic analysis in SQL. Finally, sorting and pagination are covered, and an example of retrieving the top 10 customers who made the highest total payments before October 28, 2004 is given.

02:50:00

looking at retrieving customer numbers and payment dates from a database, but rather we are interested in the total payment per customer before a certain date and sorting it by the highest total payment. To do this, the "group by" statement is used to group the customers by their ID number, and then the "sum" function calculates the total amount paid by each customer before the specified date. The resulting table is sorted by total payment amount in descending order and limited to the top 10 customers. The "offset" statement can also be used to skip a certain number of rows and look at the next set of results. Finally, the video introduces mapping functions in SQL that are applied to individual values.

02:55:00

In this section, the instructor guides the viewer on how to answer a complex question often seen in interviews. The question is to display the full name of the point of contact, in uppercase, for each customer in the United States and sort it alphabetically by customer name. The instructor breaks down the steps, starting with displaying the information about the customers and then filtering the data to only those in the United States. Then, by concatenating the first and last names of the contact and applying a mapping function, the two columns are combined and turned to uppercase. Finally, the concatenated column is renamed to contact name.

03:00:00

In this section, the instructor demonstrates how to sort and display data using SQL queries. He starts by showing how to display customer information and sort it alphabetically by their name in ascending order. He then demonstrates how to create a paginated list of customers with a country code column, which is derived from the first three letters of the country name, displayed in lowercase. Finally, he shows how to display the five most expensive products in the motorcycles product line with their prices rounded to dollars.

03:05:00

In this section of the video, the instructor demonstrates how to sort a table by a specific column and retrieve the top five values while also showing only the necessary columns. They also introduce the round function for rounding numeric data and show how to perform arithmetic operations on columns to calculate profit margin percentage. The formula for profit margin percentage is explained and applied to a products table to retrieve the top 10 products with the highest profit margin percentage, rounded to two decimal places.

03:10:00

In this section, the speaker explains how to calculate and sort profit margins in SQL. They demonstrate how to add a profit margin column, sort by it, and limit the result to show the top 10 highest profit margins. The speaker also mentions other arithmetic operations in SQL, such as rounding, and how to use them to generate complex queries. They then move on to demonstrate how to use date functions in SQL to extract specific information like years or months from a column of dates. They provide an example query that finds the largest single payment made by each customer in a specific year. The query uses grouping and sorting to obtain the result.

03:15:00

In this section, the tutorial covers how to show the total payments received month by month for every year. By selecting all the columns and creating new columns for payment year and payment month with a subsequent GROUP BY function, users can see the monthly payments for each year and order them by year and month. This allows for a clearer view of the payments received over time.

03:20:00

In this section, the tutorial covers how to format the output of SQL queries to make them presentable and easy to read. The format function for numbers is introduced, which allows the user to choose the number of decimal places and add commas for clarity. Concatenation is also explained, demonstrating how to add a dollar symbol to the output of the format function. Lastly, date format is introduced, which allows the user to convert numerical month values to text values such as January or February.

03:25:00

In this section, the instructor demonstrates how to extract the month name and number from a date using the `DATE_FORMAT` function and the `MONTH` and `PERCENTB` parameters. However, they run into an issue with ordering as they want to display the month name but order by the month number. To solve this problem, they create an additional column using the `MONTH` function and add it to the group and order by clause, but then remove it from the select clause to hide it from the output. The end result is a complex but nicely formatted revenue table that can be exported or shared with others.

03:30:00

In this section of the video, the speaker discusses joining tables in SQL. A join clause is used to combine rows from two or more tables based on a related column between them. The video presents an example of a join operation using the inner join in the context of a problem about showing the 10 most recent payments with customer details. The speaker uses the customer number column to match the information from two tables, payments and customers. They explain how to specify the join condition using the "on" keyword followed by the column names of the tables involved.

03:35:00

In this section of the tutorial, the instructor explains how to use SQL to join two tables, specifically the payments and customers tables, and select specific columns from both tables. Using the join function with the customer number column, SQL finds matching rows in the customers table and adds that information to the corresponding payment row. The instructor then goes on to demonstrate how to filter and limit the results by ordering the rows by payment date and selecting only the ten most recent payments. It is important to understand how the join function works in SQL to effectively combine data from multiple tables.

03:40:00

In this section of the tutorial, the instructor explains the differences between inner and outer joins in SQL. Inner joins only show the rows where values are present in both tables being joined, while outer joins will show information for all customers, including those without payment information (with empty data in the relevant columns). The tutorial also includes an exercise where viewers can practice joining tables to show the full office address and phone number for each employee.

03:45:00

In this section, the speaker explains the concept of joins in SQL. Specifically, the focus is on inner joins, which involves combining data from two or more tables with matching data. The speaker provides an example of how to combine the employee and office tables using inner join. Additionally, a brief overview of left join is discussed, which allows users to keep all rows from the left table even if some rows are unmatched in the right table. The speaker provides an example of a left join between the customers and orders tables, showing how it is possible to show customer information even if there is no matching order information.

03:50:00

In this section of the video, the speaker explains the concept of different types of SQL joins using examples. They demonstrate how to use left join to show all values from the left table, right join to show all values from the right table, and full outer join to retain all values from both tables. They also show how to join a table with itself using temporary aliases and provide an example of getting information about an employee's manager. The tutorial is geared towards beginners and includes technical interview questions.

03:55:00

In this section, the video walks through an inner join of the employee table with itself using temporary aliases for clarity. The join serves to obtain the information of a manager for each employee, with columns selected including employee number, last name, first name, reports to, and the concatenated first name and last name of the manager. The issue of President Diane Murphy not reporting to anyone is also addressed.

04:00:00

In this section of the YouTube tutorial, the instructor explains the concept of left, right, and full outer joins in SQL and provides examples of each type of join. The purpose of a left join is to maintain the list of all employees from the left table, even if they don't have a manager. On the other hand, a right join only shows employees from the left table who have a manager, while still displaying all rows from the right table. Finally, a full outer join shows all the rows from both tables, including those without a match in the other table. The tutorial concludes with exercises for the viewers to practice SQL joins and suggestions for improving query performance, such as creating an index.

04:05:00

In this section, the video explains the concept of indexes in SQL and how they can speed up query processing. If a column, such as contact last name, has an index, SQL can efficiently search and sort through that column using a data structure called a b-tree. The video also shows how to create a view which is a virtual table that contains information from a query. Views make it easier to query frequently used data combinations, such as customers located in the USA, and can be used just like regular tables. Lastly, the video introduces the concept of materialized views, which are similar to regular views but physically store the results of the query.

04:10:00

In this section of the video, the instructor discusses materialized views, which are a way of creating a separate table that always tracks the latest version of the joins of two tables, making it efficient to query. He explains that executing SQL queries using Python can be done through libraries such as SQL Alchemy, which is a powerful and efficient tool for working with SQL relational databases. Moreover, he introduces different relational databases, including MySQL, Postgres, and SQLite, highlighting that SQLite is a limited version of MySQL that is used in low memory settings, such as mobile apps, and will be used in the assignment.

04:15:00

database and the specific connector to use when working with SQL Alchemy, and you need to provide a connection string to create an engine for connecting to the database. The connection string contains information like the database type, connector, hostname, port, username, and password. It is recommended to check SQL tutorial websites (such as w3schools) for differences between variants of SQL, and use connectors like MySQL Python or Pi MySQL when working with specific databases that are not included in SQL Alchemy.

04:20:00

In this section, the tutorial explains how to connect to a SQL database using Python and how to execute SQL queries. The tutorial recommends installing SQL Alchemy and the connector, creating an engine that contains information about the type of server, connector, username, password, hostname, port, and database name. The tutorial also advises against typing your password directly into Jupyter Notebook and suggests using get pass to import the password, store it in a variable, and pass it into the connection string using string formatting. Once the engine is created, the connection is established, and the SQL query is executed, the tutorial explains how to convert the result into a list of tuples.

04:25:00

In this section, the video introduces the IPython SQL library, which uses SQL Alchemy to simplify connecting to SQL databases. By loading the SQL extension with `%load_ext SQL`, users can use `%sql` to establish a connection string with their preferred SQL database, without having to use explicit `import engine` commands. Instead, they can provide the database name, password, port, and so on, either by typing out the entire string or putting the connection string into a variable and passing it with brackets. Once connected, a user can execute queries using `%sql` followed by the query. Results will display in a nice tabular format, making it easier to use within Python.

04:30:00

In this section of the video, the speaker demonstrates how SQL code can be combined with Python code in a Jupyter notebook. By using the magic command "%SQL", SQL Alchemy can be used to read SQL code into Jupyter, create a connection, pass the SQL query to the connection and get the resultant data frame format. The video covers several topics in SQL including aggregation, grouping, pagination, and joins. The speaker recommends practicing with different SQL exercises to become an expert in SQL and suggests two databases to practice, the Classic Models Database and the Student University Database. Additionally, the speaker recommends checking out a repository of interview questions with SQL created by Alexi Grigurev.

04:35:00

In this section, the speaker discusses SQL and its importance for data science interviews. They provide resources for learning SQL, including a repository with over 50 technical questions, the website W3Schools, and a recommended book. Additionally, the speaker introduces a three-step approach to solving SQL interview questions, which involves understanding the inputs and outputs, articulating the solution verbally, and constructing the query step by step. The approach is exemplified by solving an SQL interview question from Apple.

04:40:00

In this section of the tutorial, the instructor discusses the approach to join two tables in SQL based on given inputs and outputs. The example given is finding details for each customer regardless of whether they made an order, where the inputs are customer ID, first name, and order details, and the output contains first name, last name, city, and order details, sorted by first name and order details. The instructor demonstrates constructing the query step-by-step, performing a left join and then ordering by the necessary columns. The second example solves an SQL interview question about calculating the difference between the highest salaries in the marketing and engineering departments. The instructor explains the input, output, and solution to the interviewer before constructing the query using aggregation functions and joins.

04:45:00

In this section, the speaker discusses two approaches to calculating the absolute highest salary difference between Departments Marketing and Engineering. The first approach involves using a join between DB Employee and DB Department and writing two select statements to calculate the maximum salary of each department and then finding the absolute difference between the two maximum salaries. The second approach involves using a self-join with table aliases and searching by department ID to calculate the highest salary difference. The speaker walks through the construction of the query step-by-step for each approach.

04:50:00

In this section, the speaker explains how to solve an SQL interview question asked by Facebook, which involves finding the popularity percentage for each user based on the total number of friends each user has compared to the total number of users on the platform. The speaker outlines their approach to calculating the total number of friends a user has and the total number of users, using the Union method and a distinct function, respectively. They then demonstrate how to construct the query step-by-step to provide the desired output, ordering the results in ascending order by user ID.

04:55:00

In this section of the SQL tutorial, the instructor demonstrates how to construct a query to calculate the total number of friends each user has and the total number of unique users on a social media platform. Using the UNION operator, duplicates are removed and the table is properly formatted to count the number of friends each user has. The total number of unique users is then calculated with a subquery and stored using a Common Table Expression. Finally, a query is constructed to calculate the popularity percentage of each user by dividing the count of their friends by the total number of unique users and multiplying by 100.

More from
freeCodeCamp.org

No videos found.

Related Videos

No related videos found.

Trending
AI Music

No music found.