The Complete MySQL Bootcamp: Zero to Hero SQL Skills
- Descrição
- Currículo
- FAQ
- Revisões
Dive into the world of SQL with our comprehensive MySQL Bootcamp, tailored to transition you from novice to expert. This course covers a robust curriculum designed for real-world applications, including both foundational and advanced SQL techniques. Start with the basics of SQL queries and rapidly advance to complex topics such as DDLs, DMLs, and Select statements. Master data manipulation and querying skills using clauses like WHERE, ORDER BY, and GROUP BY.
Enhance your SQL abilities with single-row and multi-row functions, and understand the intricacies of views, complex views, joins, and subqueries. The course also delves into vital database components such as stored procedures, cursors, CTEs, and an in-depth exploration of SQL performance optimization using various indexes and constraints.
Gain hands-on experience with an end-to-end project that includes implementing slowly changing dimensions, auditing data flows, and understanding data warehousing basics, such as normalization and schema designs. Learn to write powerful analytical queries using functions like ROW_NUMBER, RANK, and LEAD/LAG.
Additionally, this course equips you with essential industry knowledge, including MySQL installation, hardware setups, team structures in data projects, and an overview of data-related job roles such as Data Analyst and Data Engineer. Understand the software development cycle and learn how to deploy SQL code using GitHub in a production environment.
Enroll now to become proficient in MySQL and open doors to numerous opportunities in the data industry!
-
4Basic Understanding of SDLCVídeo Aula
-
5Basic Understanding of SDLCQuestionário
In this section we will help you evaluate on the basic understanding of SDLC
-
6Hardware Setup in the IndustryVídeo Aula
-
7Hardware Setup QuizQuestionário
This Quiz will help you evaluate the understanding of the hardware setup
-
8Typical Team StructureVídeo Aula
-
9Evolution of DataVídeo Aula
-
10Evolution of Data QuizQuestionário
This quiz will help you evaluate the understanding of the Evolution of Data
-
11Different Data Job RolesVídeo Aula
-
12Different Data Job RolesQuestionário
In this quiz you will get the idea about the understanding of the different job roles
-
22Understanding the Requirements for Customer TableVídeo Aula
-
23Types of Joins and where will it failVídeo Aula
-
24Practically Understand the Joins in MySQLVídeo Aula
-
25Building the logic for Customer Dimension Table - Part 1Vídeo Aula
-
26Understanding Group ByVídeo Aula
-
27Building the Logic for Customer Dimension Table - Part 2Vídeo Aula
-
30Single Row Functions - Part 1Vídeo Aula
In this comprehensive lecture, we delve into practical SQL techniques and database management essentials using the AdventureWorks database as our case study. Participants will gain hands-on experience with a variety of SQL commands and concepts that are pivotal for managing complex databases and performing advanced data manipulation. The session is structured as follows:
Database Selection and Table Queries
We will begin by learning how to switch between databases using the USE command and how to retrieve all records from a specific table. This foundational skill is crucial for navigating within SQL environments.
String Functions and Data Retrieval
Explore different string functions like CHAR_LENGTH, CHARACTER_LENGTH, and LENGTH to measure text fields within your data. We'll apply these functions to customer names and annual income fields to illustrate practical use cases in data analysis.
Table Descriptions and Schema Understanding
Understanding the structure of your tables is key to effective database management. We will use the DESC command to examine the fields and data types within the tbl_fnl_customers table, ensuring clarity in data storage and retrieval processes.
Data Concatenation and Formatting
Learn to merge and format data fields using CONCAT and CONCAT_WS for better data presentation and analysis. These techniques are essential for creating reports or exporting data that requires a specific format.
Database and Table Creation
We will cover creating a new database and setting up tables with auto-incrementing primary keys and specific data types, a critical skill for setting up new projects and storing data effectively.
Data Insertion and Order Customization
Inserting data into tables and customizing data retrieval order with conditions will be demonstrated. This section will help you manipulate and prepare data for application-specific needs.
Advanced Filtering and Data Manipulation
Advanced data filtering techniques like FIND_IN_SET and modifying data directly in queries with INSERT will be explored. These skills are valuable for managing and updating data dynamically in a database.
Practical Use Cases and Problem Solving
Through various practical examples, such as formatting numeric data, manipulating strings within URLs, and performing conditional text transformations, participants will learn to solve common data-related problems using SQL.
Special Functions and Query Optimization
The use of special SQL functions like LOCATE and LPAD for detailed text analysis and data formatting will be discussed, enhancing your ability to optimize and refine SQL queries.
This lecture is designed for intermediate to advanced participants who already have a basic understanding of SQL and wish to enhance their skills in database management and data manipulation techniques. By the end of this session, attendees will be equipped with the knowledge to efficiently manage data, perform complex queries, and handle various data transformation tasks in SQL.
-
31Single Row Functions - Part 1 (Quiz)Questionário
-
32Single Row Functions - Part 2Vídeo Aula
Join us for an engaging session where we explore the powerful string manipulation capabilities of SQL. This lecture is designed to enhance your understanding of how to effectively manage and transform textual data in databases. Using the AdventureWorks database, we will delve into a variety of essential SQL string functions that are vital for data cleaning, preparation, and analysis.
Key Learning Outcomes:
Understanding and Applying Basic String Functions:
Trimming: Learn to use LTRIM, RTRIM, and TRIM to remove unwanted whitespace from data, which is crucial for cleaning up data inputs.
Substring Extraction: Master the use of MID, SUBSTR, and SUBSTRING to extract specific portions of strings, allowing for detailed analysis and manipulation of textual data.
Advanced String Operations:
Searching and Positioning: Understand how to locate specific characters or substrings within a string using INSTR and POSITION, enabling targeted data retrieval and manipulation.
String Concatenation: Combine multiple strings into one using CONCAT and explore the utility of adding spaces with SPACE to format strings for reports or interfaces.
String Transformation and Analysis:
Modification: Learn how to replace parts of a string using REPLACE, and how to repeat strings using REPEAT, which can be useful for data formatting or generating test data.
Comparison and Conversion: Compare strings lexicographically using STRCMP and convert string cases using UPPER and LOWER, which are essential for data normalization and comparison.
Practical Examples and Use Cases:
Participants will gain hands-on experience through practical examples that demonstrate how to reverse strings with REVERSE, extract specific date components using SUBSTRING_INDEX, and adjust string length dynamically using RPAD and LPAD.
This lecture is ideal for database administrators, data analysts, and any IT professionals or students who wish to deepen their understanding of SQL’s text handling capabilities. By the end of this session, attendees will be equipped with the knowledge to effectively manipulate and analyze string data in SQL, enhancing their ability to handle real-world data challenges.
-
33Single Row Functions - Part 2Questionário
-
34Single Row Functions - Part 3 (Date Functions)Vídeo Aula
In this engaging lecture, participants will delve into the robust capabilities of SQL for manipulating and querying date and time data. Designed for database administrators, data analysts, and anyone interested in SQL, this session provides an in-depth look at how to utilize SQL's date and time functions to enhance data processing and analytics.
Key Learning Outcomes:
Introduction to SQL Date and Time Functions:
Learn the basics of SQL date and time functions and understand their critical role in data manipulation and query optimization.
Practical Application of Date Arithmetic:
Explore functions such as ADDDATE(), SUBDATE(), and DATE_ADD() to perform arithmetic operations on dates, useful for calculating deadlines, durations, and historical data analysis.
Formatting and Extracting Date Components:
Discover how to use DATE_FORMAT() to customize the presentation of date values and EXTRACT() to pull specific components from a date, enhancing the flexibility and readability of your data outputs.
Calculating Differences and Comparisons:
Master techniques to calculate differences between dates with DATEDIFF(), and understand the use of NOW(), CURDATE(), and SYSDATE() to work with current date and time, crucial for timestamping and record management.
Advanced Date Manipulation:
Delve into advanced date manipulations like determining the last day of a month with LAST_DAY(), and manipulating weeks and years with functions such as WEEK(), YEARWEEK(), and MAKEDATE().
Real-world Use Cases:
Apply learned skills to real-world scenarios, such as scheduling, reporting, and historical data comparisons, enhancing your SQL toolkit for practical database management tasks.
-
35Single Row Functions - Part 3 (Date Functions)Questionário
-
36Advanced FunctionsVídeo Aula
Conditional Expressions and Functions:
CASE: Used to implement conditional logic within SQL queries. It allows you to create different outputs based on conditions. You demonstrated this with the purchasing_power column.
IF: A function that allows you to execute a simple conditional check within an SQL query (commonly used in MySQL).
COALESCE: This function returns the first non-null value in a list of arguments. It's very useful for handling missing data by providing default values.
NULLIF: Returns NULL if the two arguments are equal; otherwise, it returns the first argument. This is helpful to prevent division by zero errors or to suppress unwanted data.
IFNULL and ISNULL: These functions check for NULL values. IFNULL (used in MySQL) returns a replacement if the expression is NULL. ISNULL (used in SQL Server) tests whether an expression is NULL, returning 1 if true and 0 if false.
Database and User Information Functions:
CURRENT_USER: Returns the user name of the current execution context. Useful for auditing who is executing the query.
DATABASE(): Retrieves the name of the current database. This is crucial in scripts that are run on multiple databases to ensure they are manipulating the correct database.
SYSTEM_USER: In SQL Server, this function returns the login name of the user. It's used for security and auditing.
USER: Similar to CURRENT_USER, it returns the database user name based on the context of the execution.
VERSION(): This function returns the current version of the SQL database system. It's important for compatibility and troubleshooting.
-
37Advanced FunctionsQuestionário
-
40Views in MySQLVídeo Aula
In this detailed lecture, we explore the concept of SQL views, a powerful feature in database management that offers both simplicity and robustness in handling data queries. By defining and utilizing views, database users can enhance both the security and efficiency of their database interactions.
Key Highlights of the Lecture:
Introduction to Views:
What are views in SQL?
The architecture and types of views.
Benefits of Using Views:
Simplification of Queries: How views can abstract complex SQL queries, making them more accessible for users.
Enhanced Security: Discuss the role of views in data security, specifically how they can limit data exposure to end users.
Improved Performance: Insights into how views can potentially optimize query performance under certain conditions.
Diving Deep into View Types:
Simple Views: Characteristics and use cases of simple views that do not contain complex SQL logic or multi-table operations.
Complex Views: Exploration of complex views involving joins, subqueries, and aggregation functions.
Practical Applications and Considerations:
Scenario-based examples illustrating when and how to effectively implement views.
Discussion on the maintenance and potential pitfalls in view implementation.
Advanced Topics:
The impact of views on database design and architecture.
Understanding the limitations and capabilities of views in real-world applications.
This lecture is designed for students who already have a basic understanding of SQL and are looking to deepen their knowledge in database management systems. Through examples, demonstrations, and detailed explanations, attendees will gain a comprehensive understanding of how views function and how they can be leveraged to improve database design and functionality.
-
41Views in MySQLQuestionário
-
42Subqueries in MySQLVídeo Aula
In this detailed lecture, we explored the concept and application of SQL subqueries within the MySQL environment. Subqueries, a powerful feature of SQL, allow us to nest queries within other queries to perform complex data retrieval in a structured and efficient manner. The focus was on the flexibility and depth that subqueries add to SQL querying, enabling refined data selection and manipulation across multiple use cases.
Topics Covered
Introduction to Subqueries: We started with an introduction to what subqueries are and how they are used in SQL. A subquery is essentially a query within another query that provides data needed for the main query to execute. This setup can be used in various parts of a SQL statement, including the SELECT, FROM, WHERE, and HAVING clauses.
Types of Subqueries: We discussed the two main types of subqueries - correlated and non-correlated. A non-correlated subquery runs independently of the outer query, returning a list or value that the outer query utilizes. In contrast, a correlated subquery refers back to elements of the outer query, running repeatedly, once for each row evaluated by the outer query.
Using Subqueries in the WHERE Clause: Extensive examples showed how subqueries could be used to filter results based on the evaluation of data contained in the database itself. For instance, finding customers who purchased a specific product by querying against a list of product keys extracted on-the-fly from a product table.
Subqueries in the FROM Clause: We looked at using subqueries within the FROM clause to create a temporary table that the outer query could then utilize. This is useful in complex data aggregation and transformation tasks, such as summarizing sales data across different years.
SELECT Clause Subqueries: The session also covered how subqueries could be used within the SELECT clause to provide detailed column-level data extraction, such as calculating averages or totals that are then used as part of the outer query's dataset.
Performance Considerations: Key to using subqueries is understanding their impact on database performance. We discussed how to optimize queries to avoid common pitfalls like redundant data processing and excessive I/O operations.
Practical Examples
Throughout the lecture, we utilized practical SQL queries to illustrate each concept:
Basic data retrieval combining multiple years of sales data to identify unique customer interactions.
Advanced filtering techniques to exclude certain categories of data and focus on specific subsets.
Aggregative subqueries to compute total sales within specified territories or under certain conditions.
Advanced Usage
We ventured into advanced topics such as creating views from subqueries to simplify complex data relationships and enhance reusability and maintenance of SQL code.
-
43Subqueries in MySQLQuestionário
-
44Common Table Expressions (CTEs)Vídeo Aula
In this lecture, we will dive deep into the concept of Common Table Expressions (CTEs) in SQL, a powerful feature that enhances the readability, maintainability, and organization of complex queries. By exploring a series of practical examples and scenarios, students will gain a comprehensive understanding of how CTEs can be used to simplify complex SQL queries, manage large datasets, and improve the performance of data retrieval operations.
Key Lecture Points:
Introduction to CTEs:
We begin by introducing the syntax and basic concept of a CTE, explaining how it serves as a temporary result set that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement. This section includes a discussion on the scope and lifecycle of a CTE within a single query execution.
Simplifying Complex Queries:
The lecture demonstrates how CTEs can break down complex queries into simpler, more manageable parts. For instance, by calculating total sales by territory and comparing these against average sales to identify high-performing regions. This approach not only clarifies the SQL code but also aids in debugging and optimizing query performance.
Advanced CTE Usage:
We explore advanced applications of CTEs, including recursive CTEs, which are useful for dealing with hierarchical data, such as organizational charts or category trees. The lecture covers practical examples like calculating cumulative sales or navigating through parent-child relationships in product categories.
Practical Scenarios:
Throughout the lecture, various practical scenarios are presented to illustrate the application of CTEs. These include:
Sales Analysis: Calculating total and average sales per territory, and identifying territories where sales exceed the average.
Customer Insights: Determining the most recent order date for each customer, counting the total orders per customer, and assessing customer value based on total revenue generated.
Product Management: Analyzing total sales and quantities sold per product, and calculating average product prices by category.
Performance Considerations:
A discussion on the performance implications of using CTEs, including when and how they should be used efficiently. This part of the lecture addresses common misconceptions and pitfalls, such as the belief that CTEs always improve performance or act as materialized views.
Hands-On Examples and Exercises:
Students will engage with real-world SQL queries employing CTEs, as shown in the lecture. They will modify and execute these queries to see firsthand how CTEs enhance query structure and clarity.
-
45Common Table Expressions (CTEs)Questionário
-
46Window Functions - Part 1Vídeo Aula
In this lecture, we explored the powerful capabilities of SQL window functions and ranking techniques, focusing on practical applications and real-world scenarios. We covered the following key topics:
Introduction to Window Functions:
Definition and importance of window functions in SQL.
Differentiation between window functions and aggregate functions.
Understanding Window Functions:
RANK(): Assigns a rank to each row within a partition, with gaps for ties.
DENSE_RANK(): Assigns a rank to each row within a partition without gaps for ties.
ROW_NUMBER(): Assigns a unique number to each row within a partition, starting at 1 for the first row.
Practical examples demonstrating how these functions can be used to rank data based on various criteria such as salary, sales, and order dates.
Advanced Window Functions:
LAG(): Retrieves data from a previous row in the same result set, useful for calculating differences between consecutive rows.
LEAD(): Retrieves data from a subsequent row in the same result set, useful for comparing current and future values.
Usage scenarios for LAG() and LEAD(), including financial data analysis and trend identification.
Partitioning Data:
Explanation of the PARTITION BY clause and its role in dividing the result set into partitions for window functions.
Examples showing how partitioning affects the outcome of window functions, such as calculating ranks within specific regions or categories.
Order and Partition:
Importance of the ORDER BY clause in window functions to determine the order of rows within each partition.
How combining PARTITION BY and ORDER BY can provide more granular insights into data.
Real-World Applications:
Practical SQL queries that demonstrate the use of window functions in various business scenarios, such as:
Ranking customers by annual income.
Identifying the top 5 products by sales.
Calculating year-over-year revenue growth.
Determining the first order for each customer.
Ranking orders by the total number of items.
Hands-On SQL Query Examples:
Step-by-step walkthrough of SQL queries using RANK(), DENSE_RANK(), ROW_NUMBER(), LAG(), and LEAD().
Analysis of query outputs to understand the effects of different window functions and their combinations.
-
47Window Functions - Part 1Questionário
-
48Window Functions - Part 2Vídeo Aula
Overview
Welcome to the course on advanced SQL techniques in MySQL! This course is designed to help you master the concepts of moving averages, moving sums, and framing using window functions. These powerful tools are essential for data analysis, enabling you to uncover trends, patterns, and insights in your data.
What You'll Learn
1. Moving Averages:
Understand the concept of moving averages and how they help smooth out short-term fluctuations in data.
Learn how to calculate moving averages in MySQL using window functions.
Explore practical examples and use cases where moving averages provide valuable insights.
2. Moving Sums:
Discover the importance of moving sums in cumulative data analysis.
Learn to calculate moving sums over specific subsets of data.
Apply moving sums to real-world scenarios to track cumulative performance over time.
3. Framing in SQL:
Gain a deep understanding of framing and its role in window functions.
Learn how to define and manipulate the subset of rows (frame) for window function calculations.
Explore different framing options, such as unbounded preceding, current row, and specific ranges.
4. Practical Applications:
Implement moving averages and moving sums in various business contexts, such as sales analysis, financial forecasting, and performance monitoring.
Use window functions to segment data and identify trends, outliers, and performance patterns.
-
49Window Functions - Part 2Questionário
-
50Data Issues & Loading Staging Tables (Again) with revised Business RequirementVídeo Aula
-
51Loading Customer Table and Audit Table using Stored ProcedureVídeo Aula
-
52Loading Customer Table and Audit Table using Stored ProcedureQuestionário
-
53Loading Other HOP1 TablesVídeo Aula
-
54Loading Hop2 TablesVídeo Aula
-
55Loading Final Tables and Understanding the ConstraintsVídeo Aula
-
56Loading the Customer Data coming from External ApplicationVídeo Aula
-
57Loading the Customer Data coming from External ApplicationQuestionário
-
58Understanding Slowly Changing Dimensions (SCDs)Vídeo Aula
In this lecture, we will explore the concept of Slowly Changing Dimensions (SCD), focusing on three primary types: SCD1, SCD2, and SCD3. Slowly Changing Dimensions are crucial in data warehousing for managing and tracking changes in dimension data over time, ensuring data integrity and accurate historical reporting.
Topics Covered:
Introduction to Slowly Changing Dimensions (SCD):
Definition and importance in data warehousing.
Overview of dimension data and the need for managing changes.
SCD Type 1:
Definition: Simple approach where old data is overwritten with new data.
Implementation: Direct update of dimension records.
Pros: Easy to implement and maintain, saves storage space, simplifies queries.
Cons: Loss of historical data, not suitable for scenarios requiring historical analysis.
SCD Type 2:
Definition: Tracks historical data by creating multiple records for each dimension entity.
Implementation: Adding new records with different versions or timestamps.
Pros: Preserves complete historical data, enables trend analysis and historical reporting.
Cons: Increases table size, potentially impacts performance, more complex queries.
SCD Type 3:
Definition: Tracks limited historical data by adding new columns for previous values.
Implementation: Adding columns to store previous values and updating them when changes occur.
Pros: Easy to implement, requires less storage, provides some historical data.
Cons: Limited historical data, not suitable for detailed historical analysis.
Comparative Analysis:
Discussing the pros and cons of each SCD type.
Determining which SCD type is suitable for different business scenarios.
By the end of this lecture, you will have a comprehensive understanding of SCD1, SCD2, and SCD3, their implementations, and the advantages and disadvantages of each. This knowledge will help in designing effective data warehousing solutions that meet the specific needs of your organization.
-
59Understanding Slowly Changing Dimensions (SCDs)Questionário
-
60Understanding Cursors and implementing it in MySQLVídeo Aula
In this lecture, we delved into the practical applications of SQL procedures and cursors, focusing on various use cases involving employee data management. Below is an overview of the key procedures we covered, their functionalities, and the concepts they illustrate:
Aggregate Salaries:
Procedure Name: AggregateSalaries
Description: This procedure calculates the total sum of all employee salaries in the employees table.
Key Concepts:
Cursor Declaration: Used to iterate over employee salaries.
Aggregation: Summing up salaries to get a total.
Cursor Handling: Opening, fetching data, and closing the cursor.
Control Structures: Using loops and conditionals to manage the flow.
Delete Low Salary Employees:
Procedure Name: DeleteLowSalaryEmployees
Description: This procedure removes employees whose salaries are less than 55,000 from the employees table.
Key Concepts:
Conditional Deletion: Deleting rows based on a condition.
Cursor Operations: Fetching and processing data row by row.
Exception Handling: Managing end-of-data conditions with handlers.
List Employees:
Procedure Name: ListEmployees
Description: This procedure lists the names of all employees in the employees table.
Key Concepts:
Data Retrieval: Using cursors to fetch and display employee names.
Loop Constructs: Iterating through the result set until all rows are processed.
Process Salaries:
Procedure Name: ProcessSalaries
Description: This procedure categorizes employees based on their salary, identifying whether they have a "high salary" or a "normal salary".
Key Concepts:
Data Categorization: Conditional logic to classify salaries.
Dynamic Messages: Generating output messages based on data values.
Real-time Processing: Immediate processing and output of each employee's salary status.
Update Salaries:
Procedure Name: UpdateSalaries
Description: This procedure increases the salaries of employees earning 55,000 or less by 2,000.
Key Concepts:
Conditional Updates: Applying updates to specific rows based on conditions.
Incremental Adjustments: Modifying data values dynamically.
Cursor Management: Iterating over rows to apply changes.
Employee Slowly Changing Dimensions (SCD):
Procedure Name: Employee_SCD
Description: This procedure maintains historical versions of employee data, managing slowly changing dimensions (SCD) by recording changes in the employee_scd table.
Key Concepts:
Historical Data Management: Tracking changes over time and maintaining versioned records.
Conditional Logic: Checking for existing active records and updating status.
Data Insertion: Adding new versions of records with updated information.
Data Cleanup: Truncating the staging table to prepare for new data.
Key Learning Points:
Cursors in SQL:
Cursors allow row-by-row processing of query results, making it possible to perform complex data manipulations that require iterative operations.
Exception Handling:
Using handlers for managing end-of-data conditions (NOT FOUND) and other exceptions ensures robust and error-free procedures.
Control Structures:
Implementing loops (LOOP, LEAVE) and conditionals (IF, ELSE) to control the flow of operations within procedures.
Data Aggregation and Transformation:
Aggregating data using summation, updating rows conditionally, and transforming data dynamically during the cursor processing.
Maintaining Historical Data:
Techniques for handling slowly changing dimensions, essential for applications requiring historical data tracking and version control.
Best Practices:
Ensuring cursors are properly opened and closed to avoid resource leaks.
Using clear and descriptive comments in code to improve readability and maintainability.
Structuring SQL procedures to handle real-world business logic effectively.
Practical Applications:
Payroll Management: Automating salary calculations, updates, and classifications.
Data Cleaning: Removing outdated or irrelevant data based on business rules.
Historical Data Analysis: Maintaining detailed records of changes over time for auditing and analysis.
-
61Implementing SCD2 on Customer DatasetVídeo Aula
-
62Loading Sales Data Coming from External ApplicationVídeo Aula
The lecture aimed to explain the concept, creation, and usage of temporary tables in MySQL. The focus was on demonstrating how temporary tables can be used in stored procedures to manage intermediate data transformations in a sales application scenario.
Key Concepts Covered:
Introduction to Temporary Tables:
Definition and purpose of temporary tables.
Differences between temporary tables and regular tables.
Scope and lifecycle of temporary tables.
Creating Temporary Tables:
Syntax for creating temporary tables.
Examples of creating temporary tables within stored procedures.
Using Temporary Tables:
Inserting data into temporary tables.
Selecting data from temporary tables.
Joining temporary tables with other tables.
Dropping Temporary Tables:
Automatically dropped when the session ends.
Manually dropping temporary tables if necessary.
-
63Loading Sales Data Coming from External ApplicationQuestionário
-
64Performance Tuning - Part 1Vídeo Aula
Introduction
In this lecture, we covered the essential concepts of indexing in SQL databases and various performance tuning techniques. We discussed three primary types of indexes: clustered index, non-clustered index, and B-tree index. We also reviewed some SQL queries to understand how indexing and performance tuning can improve query execution times.
Indexes in SQL
Clustered Index
A clustered index determines the physical order of data in a table. There can only be one clustered index per table because the data rows themselves can only be sorted in one order.
The primary key of a table is often the clustered index.
Non-Clustered Index
A non-clustered index does not alter the physical order of the data in the table. Instead, it creates a separate object within the table that points back to the original table rows after searching.
You can have multiple non-clustered indexes on a single table.
B-Tree Index
B-Tree (Balanced Tree) indexes are the most common type of index in SQL databases. They provide a balanced structure, ensuring that all leaf nodes are at the same level, which helps in efficient searching, insertion, and deletion.
Both clustered and non-clustered indexes can be implemented using B-Trees.
-
65Performance Tuning - Part 2Vídeo Aula
-
68On Delete CascadeVídeo Aula
Using ON DELETE CASCADE:
Concept: Referential Actions
Description: Understand how ON DELETE CASCADE works to automatically delete related records in child tables when a record in the parent table is deleted.
Scenario: Deleting a customer and automatically deleting all their orders.
-
69Update JoinVídeo Aula
Updating Data with Joins:
Command: UPDATE ... INNER JOIN
Description: Perform updates on a table using data from another table through a join.
Example: Adjusting sales amounts by applying discounts from the products table.
-
70Delete JoinVídeo Aula
Deleting Customers without Sales Records:
The DELETE statement with a LEFT OUTER JOIN finds customers who do not have any associated sales records (i.e., sales.sale_id IS NULL).
DELETE c FROM customers c LEFT OUTER JOIN sales s ON c.customer_id = s.customer_id WHERE s.sale_id IS NULL; deletes those customers from the customers table (using alias c).
-
71Set OperatorsVídeo Aula
Creating and Using the Database:
CREATE DATABASE misc; creates a new database named misc.
USE misc; selects the misc database for subsequent operations.
Creating Tables:
Customers India Table: Defines the structure of the customers_india table with columns customer_id, customer_name, and email.
Customers US Table: Defines the structure of the customers_us table with columns customer_id, customer_name, and email.
Inserting Data:
Customers India: Adds five customers with names and unique email addresses.
Customers US: Adds five customers with names and unique email addresses.
Retrieving Data:
SELECT * FROM customers_india; retrieves and displays all records from the customers_india table.
SELECT * FROM customers_us; retrieves and displays all records from the customers_us table.
Finding Intersections:
All Columns Intersection:
SELECT * FROM customers_us INTERSECT SELECT * FROM customers_india; finds the intersection of all columns between the two tables.
Specific Columns Intersection:
SELECT customer_name, email FROM customers_india INTERSECT SELECT customer_name, email FROM customers_us; finds the intersection based on customer_name and email between the two tables.
Performing Joins:
Inner Join:
SELECT * FROM customers_us cu INNER JOIN customers_india ci ON cu.customer_name = ci.customer_name; performs an inner join to find customers with the same names in both tables.
Finding Differences:
EXCEPT Clause:
SELECT customer_name, email FROM customers_india EXCEPT SELECT customer_name, email FROM customers_us; finds customers present in customers_india but not in customers_us.
LEFT OUTER JOIN:
SELECT * FROM customers_india ci LEFT OUTER JOIN customers_us cu ON cu.customer_name = ci.customer_name WHERE cu.customer_id IS NULL; finds customers present in customers_india but not in customers_us using a LEFT OUTER JOIN.
-
72Self JoinVídeo Aula
Selecting the Database:
USE hrdb; selects the hrdb database for subsequent operations.
Retrieving Employee Data:
SELECT employee_id, employee_name, manager_id FROM tbl_fnl_employees; retrieves and displays all records from the tbl_fnl_employees table to see the current data, specifically the employee_id, employee_name, and manager_id.
Performing a Self Join:
The SELECT statement with a self join retrieves employee details along with their manager's name.
emp is an alias for the tbl_fnl_employees table representing employees.
mgr is an alias for the same table representing managers.
LEFT OUTER JOIN is used to include all employees, even if they do not have a manager.
The join condition ON emp.manager_id = mgr.employee_id matches each employee's manager_id with the employee_id of other employees in the same table.
The result includes the employee's employee_id, employee_name, manager_id, and the manager_name.