Top 5 SQL Mistakes You Must Avoid!

Manish Sharma · Beginner ·📊 Data Analytics & Business Intelligence ·2y ago

Key Takeaways

Avoids common SQL mistakes, such as incorrect use of null and handling null in aggregate functions, using PL/SQL

Full Transcript

these are five common sequel mistakes that nobody's talking about so without wasting any time let's start the tutorial mistake number one incorrect use of null null represents the absence of a value or unknown data in a database it is not the same as empty string or zero in simple terms it means that the value is missing or undefined for example I have a table named example uncore table with four rows of data as you can see there are a lot of null values in this data let's explore some common SQL mistakes and see how we can avoid them first checking for null values we usually use the equal to operator to check for null values like this if we run this query no rules will be returned let's execute as you can see nothing has been returned does this means we don't have any null values in the discount column no to check for null values we should use the is null logical operator instead of equal to operator like this this simply replace the equal to sign with the isal operator that's it let's execute this query and now you can see we have two rowes where the value for the discount column is null handling null in aggregate function as mentioned earlier null is neither an empty string nor zero so we need to handle it carefully with aggregate functions so for example consider this query the result shows that the sum of sales uncore amount is 450 while the average of commission is 7.5 this result is inaccurate because we did not handle null values properly here for instance there are four entries and among those there are two entries for commission 10 and 15 if we calculate the average it should be 3.75 but here it is showing 7.5 this happens because null entries are ignored in the calculation therefore we need to be careful with how we handle null values here is how to deal with null values while using aggregate functions in this query the co Zac function return the first nonn null value which will be the value stored in the column or zero if the value is null let's execute this query and now you can see we got the correct answer similarly there are many other examples comment if you want me to do a separate video on how to handle null properly mean why let's jump to the next mistake mistake number two incorrect use of distinct keyword we use the distinct keyword to find the unique values stored in a column but sometimes it yields inaccurate results let's take an example here I have a table named 01 with data for five employees among these five employees the name John appears twice however both entries are unique in their own way Joan has two job titles tester and developer if we use distinct on the first name we won't be able to uncover this detail so a proper investigation is required before using distinct keyword first find out which entries are duplicates using the group by Clause this will show the duplicate entries next examine which other columns the duplicate entries depend on in our case it's the job uncore title let's check the job titles and see how many job titles are mapped against each first name when we execute this we see that the name John is associated with two job titles this way we get the distinct first names and clearer picture of why the names is repeating it helps determine if it's really a duplicate or if it's the same JN with different job titles or possibly two different J using distinct can be slow for large data sets because it needs to sort and remove duplicates on the other hand Group by can be more efficient if you need to aggregate data and it's also clearer in terms of intent overusing distinct is a common SQL mistake that can mask underlying data issues and lead to inefficient queries you can write more efficient and meaningful SQL queries by understanding the root cause of duplicates and using group bu for aggregation or data cleanup let's move on to the next mistake mistake number three not handling errors suppose we are updating data into a table let's see if the first update statement succeeds but the second update statement fails the data is left in an inconsistent State there is no mechanism to roll back the changes if an error occurs the therefore we should handle error properly here if any error occurs now it will be handled and transaction will be rolled back to the save point this is a dummy structure of error handling I have done a video on error handling in Oracle database you can find its Link in the description or in the I button on your screen mistake number four failing to normalize data here we have a table definition can you guess what's wrong here this is a denormalized table it has employee related attributes as well as Department related attributes when creating this table the first problem is redundancy the department uncore name is repeated for every employee in the same Department this redundency can lead to inconsistent data and will consume a lot of storage the second problem is update anomalies for example if your table has millions of entries and your company decides to change the department name from HR to Human Resources this update requires modifying a huge number of rules which consumes resources and is error PR next are insertion anomalies for example if your company decides to add a new Department named AI but hasn't hired anyone for it yet you cannot insert that department data into this table to create a new Department you need data for other columns first for example to add the department name AI to to this table you need to enter an employee ID because it's a primary key but since no one has been hired yet you cannot add an employee ID similarly there are deletion anomalies Suppose there were five employees in the HR department and four have already left the company if the fifth one also decides to leave and you delete their record the data for the HR department will also be deleted losing the department record along with the employee records the solution is to normalize the table simply create two tables one for employee data and another for Department data if you want a dedicated tutorial on normalization comment and let me know this is one of the most common mistakes people make while it might seem convenient it can lead to inefficiency and other issues especially in large systems retrieving all columns can be inefficient especially if the table has many columns and only a few are needed if the table has many columns you might be transferring a lot of unnecessary data which can slow down the query that database needs to read more data from disk increasing input output operations the best approach is to retrieve only the necessary data and use the wear Clause to filter the data you need this will decrease the the load on the database by reducing the amount of data feted from the disk the benefits of not using select estri are it immediately clear which columns are being used second only the needed data is retrieved improving performance these are five common SQL mistakes people make in this tutorial I have not touched on the topic of indexes because that requires an entire video I will cover that soon that's it for this tutorial make sure sure to subscribe to catch the next interesting topic thanks for watching this is Manish from rebellionrider.com

Original Description

5 SQL Mistakes You DIDN'T Know You Were Making! 🎯Explore the complete PL/SQL course for FREE on my website at https://www.rebellionrider.com/category/pl-sql/ ============ Watch how to configure Oracle on VS Code https://youtu.be/6mwAx4sGhwk ============ The camera gear I use in my Videos https://www.amazon.in/shop/manishsharma?listId=DU9UM0XL97KM&ref=idea_share_inf ============ Connect With Me on My Social Media https://www.instagram.com/RebellionRider/ https://www.facebook.com/TheRebellionRider/ https://twitter.com/RebellionRider https://www.linkedin.com/in/mannbhardwaj/ ============ FAQ Which book to refer to learn - PL/SQL https://amzn.to/2QE1jX0 Performance Tuning https://amzn.to/2sgiAw4 1z0-071 Exam https://amzn.to/2sgfeJw Python Programming https://amzn.to/305UEbh ============ AFFILIATE DISCLOSURE: Some of the links used in the description will direct you to Amazon.in. As an Amazon Associate, I earn from qualifying purchases at no additional cost to you. #rebellionrider ============= Learn about the 5 common SQL mistakes that often go unnoticed and how to avoid them! In this tutorial, we'll cover errors such as the incorrect use of NULL, misuse of the DISTINCT keyword, not handling errors properly, failing to normalize data, and using SELECT * inappropriately. Enhance your SQL skills and ensure your queries are efficient, accurate, and optimized. 💡 Key Takeaways: Correctly check for NULL values Proper use of DISTINCT and GROUP BY Efficient error handling techniques Importance of database normalization Optimizing SELECT queries for better performance 📺 Subscribe for more SQL tutorials and stay ahead in your tech journey! 🚀 #DataAnalysis
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Manish Sharma · Manish Sharma · 0 of 60

← Previous Next →
1 Oracle Database tutorials 1: How to install Oracle Database 11g on windows 7
Oracle Database tutorials 1: How to install Oracle Database 11g on windows 7
Manish Sharma
2 Oracle Database tutorials 2:How To install SQL Developer on windows 7
Oracle Database tutorials 2:How To install SQL Developer on windows 7
Manish Sharma
3 Oracle Database tutorials 3:How to enable Line numbers in SQL Developer.
Oracle Database tutorials 3:How to enable Line numbers in SQL Developer.
Manish Sharma
4 Oracle Database tutorials 4:  database connectivity using SQL developer and command prompt
Oracle Database tutorials 4: database connectivity using SQL developer and command prompt
Manish Sharma
5 Oracle Database tutorials 5:  how to Fetch Data using SELECT - SQL statement by Manish Sharma
Oracle Database tutorials 5: how to Fetch Data using SELECT - SQL statement by Manish Sharma
Manish Sharma
6 Oracle Database11g tutorials 6 | | How to use Concatenation operator, character String
Oracle Database11g tutorials 6 | | How to use Concatenation operator, character String
Manish Sharma
7 Oracle Database11g tutorials 7 | |SQL DISTINCT keyword || SQL tutorials
Oracle Database11g tutorials 7 | |SQL DISTINCT keyword || SQL tutorials
Manish Sharma
8 Canon EOS 600D 2 lens kit/Canon rebell EOS T3i 2 lens kit Unboxing
Canon EOS 600D 2 lens kit/Canon rebell EOS T3i 2 lens kit Unboxing
Manish Sharma
9 First look: ORACLE CERTIFIED ASSOCIATE (OCA) CERTIFICATE  - ORACLE DATABASE ADMINISTRATOR
First look: ORACLE CERTIFIED ASSOCIATE (OCA) CERTIFICATE - ORACLE DATABASE ADMINISTRATOR
Manish Sharma
10 Oracle Database11g tutorials 8 || SQL DISTINCT with multiple columns |SQL Distinct with Two columns
Oracle Database11g tutorials 8 || SQL DISTINCT with multiple columns |SQL Distinct with Two columns
Manish Sharma
11 Oracle Database11g tutorials 9 || What is archive log mode and how to enable archive log mode
Oracle Database11g tutorials 9 || What is archive log mode and how to enable archive log mode
Manish Sharma
12 Oracle Database11g tutorials 10 ||  SQL Single Row Function (SQL Functions )
Oracle Database11g tutorials 10 || SQL Single Row Function (SQL Functions )
Manish Sharma
13 Oracle Database11g tutorials 11: SQL case manipulation function in Oracle Database
Oracle Database11g tutorials 11: SQL case manipulation function in Oracle Database
Manish Sharma
14 how to add channel trailer and section on your youtube channel 2014
how to add channel trailer and section on your youtube channel 2014
Manish Sharma
15 Oracle Database11g tutorials 12 || SQL Concat Function - SQL character manipulation function
Oracle Database11g tutorials 12 || SQL Concat Function - SQL character manipulation function
Manish Sharma
16 Oracle Database11g tutorials 13 || SQL substr function / SQL substring function
Oracle Database11g tutorials 13 || SQL substr function / SQL substring function
Manish Sharma
17 Oracle Database11g tutorials 14 : How to CREATE TABLE using sql developer and command prompt
Oracle Database11g tutorials 14 : How to CREATE TABLE using sql developer and command prompt
Manish Sharma
18 SQL tutorials 15 || How To CREATE TABLE using enterprise manager 11g
SQL tutorials 15 || How To CREATE TABLE using enterprise manager 11g
Manish Sharma
19 Oracle Database11g tutorials 16: How to uninstall oracle 11g from windows 7 64 bit
Oracle Database11g tutorials 16: How to uninstall oracle 11g from windows 7 64 bit
Manish Sharma
20 ORACLE CERTIFIED PROFESSIONAL(OCP) CERTIFICATE First look  - ORACLE DATABASE ADMINISTRATOR
ORACLE CERTIFIED PROFESSIONAL(OCP) CERTIFICATE First look - ORACLE DATABASE ADMINISTRATOR
Manish Sharma
21 Plantronics audio 655 USB headset with Mic Unboxing and Review and Plantronics audio 655 Mic test
Plantronics audio 655 USB headset with Mic Unboxing and Review and Plantronics audio 655 Mic test
Manish Sharma
22 SQL tutorials 17: SQL Primary Key constraint,  Drop primary Key
SQL tutorials 17: SQL Primary Key constraint, Drop primary Key
Manish Sharma
23 SQL tutorials 18: SQL Foreign Key Constraint By Manish Sharma
SQL tutorials 18: SQL Foreign Key Constraint By Manish Sharma
Manish Sharma
24 SQL tutorial 19: ON DELETE SET NULL clause of Foreign Key By Manish Sharma (RebellionRider)
SQL tutorial 19: ON DELETE SET NULL clause of Foreign Key By Manish Sharma (RebellionRider)
Manish Sharma
25 SQL tutorials 20: On Delete Cascade Foreign Key By Manish Sharma (RebellionRider)
SQL tutorials 20: On Delete Cascade Foreign Key By Manish Sharma (RebellionRider)
Manish Sharma
26 SQL tutorial 21: How To Rename Table in SQL using ALTER TABLE statement By Manish Sharma
SQL tutorial 21: How To Rename Table in SQL using ALTER TABLE statement By Manish Sharma
Manish Sharma
27 SQL tutorial 22: How to Add / Delete column from an existing table using alter table
SQL tutorial 22: How to Add / Delete column from an existing table using alter table
Manish Sharma
28 SQL tutorial 23: Rename and Modify Column Using Alter Table By Manish Sharma (RebellionRider)
SQL tutorial 23: Rename and Modify Column Using Alter Table By Manish Sharma (RebellionRider)
Manish Sharma
29 SQL tutorial 24 :SQLJoins- Natural Join With ON and USING clause By Manish/Rebellionrider
SQL tutorial 24 :SQLJoins- Natural Join With ON and USING clause By Manish/Rebellionrider
Manish Sharma
30 Oracle Database11g tutorials 25: How to install Oracle Database 11g Express Edition R2 on Windows 7
Oracle Database11g tutorials 25: How to install Oracle Database 11g Express Edition R2 on Windows 7
Manish Sharma
31 SQL tutorial 26: Introduction to SQL Joins in Oracle Database
SQL tutorial 26: Introduction to SQL Joins in Oracle Database
Manish Sharma
32 Vidcon 2014 YouTube Fan Funding: How to enable fan funding on YouTube Channel
Vidcon 2014 YouTube Fan Funding: How to enable fan funding on YouTube Channel
Manish Sharma
33 SQL tutorial 27: Right Outer Join in SQL by Manish Sharma for RebellionRider
SQL tutorial 27: Right Outer Join in SQL by Manish Sharma for RebellionRider
Manish Sharma
34 SQL tutorial 28: Left Outer Join By Manish Sharma / RebellionRider
SQL tutorial 28: Left Outer Join By Manish Sharma / RebellionRider
Manish Sharma
35 SQL tutorial 29: Full Outer Join with example By Manish Sharma/ RebellionRider
SQL tutorial 29: Full Outer Join with example By Manish Sharma/ RebellionRider
Manish Sharma
36 SQL tutorial 30: Inner Join In SQL by Manish Sharma/RebellionRider
SQL tutorial 30: Inner Join In SQL by Manish Sharma/RebellionRider
Manish Sharma
37 SQL tutorial 31 : SQL Cross Join In Oracle Database By Manish Sharma from RebellionRider
SQL tutorial 31 : SQL Cross Join In Oracle Database By Manish Sharma from RebellionRider
Manish Sharma
38 SQL tutorial 32: How To Insert Data into a Table Using SQL Developer
SQL tutorial 32: How To Insert Data into a Table Using SQL Developer
Manish Sharma
39 SQL tutorial 33:How To Insert Data into a Table Using SQL INSERT INTO dml statement
SQL tutorial 33:How To Insert Data into a Table Using SQL INSERT INTO dml statement
Manish Sharma
40 SQL tutorial 34: How to copy /Insert data into a table from another table using INSERT INTO SELECT
SQL tutorial 34: How to copy /Insert data into a table from another table using INSERT INTO SELECT
Manish Sharma
41 SQL tutorial 35: DELETE and TRUNCATE how to delete data from a table
SQL tutorial 35: DELETE and TRUNCATE how to delete data from a table
Manish Sharma
42 SQL tutorial 36: how to create database using database configuration assistant DBCA
SQL tutorial 36: how to create database using database configuration assistant DBCA
Manish Sharma
43 SQL tutorial 37: How to create NEW USER account using Create User statement in Oracle database
SQL tutorial 37: How to create NEW USER account using Create User statement in Oracle database
Manish Sharma
44 SQL tutorial 38: How to create user using SQL Developer in Oracle database
SQL tutorial 38: How to create user using SQL Developer in Oracle database
Manish Sharma
45 SQL tutorial 39: How to create user in oracle using Enterprise Manager
SQL tutorial 39: How to create user in oracle using Enterprise Manager
Manish Sharma
46 SQL tutorial 40: DBA Trick, How to drop a user when it is connected to the database
SQL tutorial 40: DBA Trick, How to drop a user when it is connected to the database
Manish Sharma
47 Motorola Moto G 2nd Generation / G2 Unboxing and Review
Motorola Moto G 2nd Generation / G2 Unboxing and Review
Manish Sharma
48 SQL tutorial 41: How to UNLOCK USER in oracle Database
SQL tutorial 41: How to UNLOCK USER in oracle Database
Manish Sharma
49 SQL tutorial 42: How to Unlock user using SQL Developer By Manish Sharma RebellionRider
SQL tutorial 42: How to Unlock user using SQL Developer By Manish Sharma RebellionRider
Manish Sharma
50 SQL tutorial 43: How to create an EXTERNAL USER in oracle database By Manish Sharma RebellionRider
SQL tutorial 43: How to create an EXTERNAL USER in oracle database By Manish Sharma RebellionRider
Manish Sharma
51 SQL tutorial 44: How to import data from Microsoft Excel to Oracle Database using SQL Developer
SQL tutorial 44: How to import data from Microsoft Excel to Oracle Database using SQL Developer
Manish Sharma
52 SQL tutorial 45: Introduction to user Privileges in Oracle Database By Manish Sharma RebellionRider
SQL tutorial 45: Introduction to user Privileges in Oracle Database By Manish Sharma RebellionRider
Manish Sharma
53 SQL tutorial 46: What are System Privileges & How To Grant them using Data Control Language
SQL tutorial 46: What are System Privileges & How To Grant them using Data Control Language
Manish Sharma
54 SQL tutorial 47: How to Grant Object Privileges With Grant Option in Oracle Database
SQL tutorial 47: How to Grant Object Privileges With Grant Option in Oracle Database
Manish Sharma
55 SQL tutorial 48: How to create Roles in Oracle Database
SQL tutorial 48: How to create Roles in Oracle Database
Manish Sharma
56 SQL tutorial 49: CASE - Simple Case Expression in Oracle Database (1/2)
SQL tutorial 49: CASE - Simple Case Expression in Oracle Database (1/2)
Manish Sharma
57 SQL tutorial 50: CASE - Searched Case Expression In Oracle (2/2)
SQL tutorial 50: CASE - Searched Case Expression In Oracle (2/2)
Manish Sharma
58 SQL tutorial 51: DECODE function in Oracle Database By Manish Sharma (RebellionRider)
SQL tutorial 51: DECODE function in Oracle Database By Manish Sharma (RebellionRider)
Manish Sharma
59 Oracle Database Tutorial 52 : Data Pump expdp - How to Export full database using expdp
Oracle Database Tutorial 52 : Data Pump expdp - How to Export full database using expdp
Manish Sharma
60 Oracle Database Tutorial 53 : Data pump expdp - How to Export tablespace in Oracle Database
Oracle Database Tutorial 53 : Data pump expdp - How to Export tablespace in Oracle Database
Manish Sharma

Related Reads

📰
How I check duplicate IDs and missing cells in CSV with Python
Learn to check duplicate IDs and missing cells in CSV files using Python's standard library
Dev.to · Kalamari0227
📰
ClickHouse with Examples in FastAPI and Golang: A Practical Guide for High-Performance Analytics
Learn to use ClickHouse for high-performance analytics with examples in FastAPI and Golang
Medium · Python
📰
The Fermi Paradox: Why a Crowded Universe Looks Empty
Explore the Fermi Paradox and its implications on the search for life in the universe, and why it matters for astrobiology and the future of space exploration
Medium · Data Science
📰
OpenPowerlifting Veri Seti ile Keşifçi Veri Analizi (EDA): Domain Bilgisinin Önemi ve Anomali…
Learn how to apply exploratory data analysis (EDA) on a real-world dataset, OpenPowerlifting, and understand the importance of domain knowledge in data science
Medium · Data Science
Up next
This could be the most perfect data frontend
Matt Williams
Watch →