Row_number in SQL is a window function which basically generates a unique positive integer number against a every rows of a table. It is widely used in SQL irrespective of type of databases, whether it is a SQL server, Mysql, PostgreSQL etc etc.
Table of Contents
Use Case of ROW_NUMBER :
Whenever you need to get a unique identity of a row you can use this window function to get a row number and based on that row number you can perform your task. It generates on run time and will not be saved in your table.
In this post I am going to give you the ultimate 10 ways to remove duplicates in SQL in two scenario along with a youtube video in hindi where I have executed all the queries.
Please open this in desktop mode for better experience because code panel will be viewed better in desktop mode.
I have shown the methods how to delete all duplicate record except one record from the table and except the solution no. 5 & 3( scenario 2 ) we can use all the methods to delete duplicates in production environment as well.
Table of Contents
What is Deduplication in SQL?
Deduplication basically refers to a method of eliminating the duplicate data from a dataset or table. It is very important task to do so especially for any task from where you going to get some insights.
Duplicate data from a table can change the whole theme. There are many ways to remove redundant data from a table or data set. I have explained the 10 ways to remove duplicates in SQL in two scenario.
How Duplicate Happens
Duplicates can be entered in a table in many ways like each and every column will be duplicated, I mean the whole row can be duplicated or there may have a column which will unique but rest columns data will be duplicate.
Scenario 1 :
At least one column having unique record. Here in my scenario a table called “students” having columns id, name,age,class and the column id is unique.
Sol 1 : Using unique identifier
delete from students
where id in(
select max(id) from students group by
name,age having count(*)>1)
Sol 2 : Using self join
delete from students where id in
(select s2.id from students s1 join
students s2 on s1.name=s2.name and
s1.age=s2.age where s1.id<s2.id )
Sol 3 : Using min function. It will remove multiple duplicates
delete from students where id not in(
select min(id) from students
group by name,age )
Sol 4 : Using window function ( row_number() )
delete from students where id in
(select id from
(select *, row_number()
over(partition by name,age) as rn from students)
where rn>1)
Here is the detailed post where row_number() explained, you can have a look.
Sol 5 : Using backup table
create table stu
as
select * from students where 1=2 --< table creation
insert into stu
select min(id) from students
group by name,age --< Data insertion
drop table students --< Drooping old table
alter table stu
rename to students --< Renaming new table in as old table name
Sol 6 : Using backup table without dropping original table
create table stu
as
select * from students where 1=2 --< table creation
insert into stu
select min(id) from students
group by name,age --< Data insertion
truncate table students --< Deleting data from old table
insert into students
select * from students --< Inserting data to old table from backup table
Scenario 2 :
Where the whole row is duplicated including id column.
Sol 1 : Using CTID ( ROWID in Oracle SQL )
delete from students
where ctid not in(
select min(ctid) from students
group by name,age)
Sol 2 : By adding a temporary column in current column
alter table students add column
row_num int generated always as identity --< Adding new column
delete from students where
row_num not in(
select min(row_num) from students
group by name,age) --< Removing duplicates
alter table students drop column row_num --< Dropping newly added column
Sol 3 : Using backup table
create table stu
as
select * from students where 1=2 --< Creating backup table
insert into stu
select distinct * from students --< Inserting unique record to new table
drop table students --< Dropping old table
alter table stu rename to students --< Renaming new table
Sol 4 : Using backup table without dropping original table
create table stuu as
select distinct * from students --< Creating backup table
truncate table students --< Deleting rows of old table
insert into students
select * from stuu --< inserting data to old table from backup table
The youtube video explaining the 10 ways to remove duplicates in SQL in hindi
In this video I have explained in detail ” How to remove duplicates in SQL in hindi
Frequently Asked Questions :
How to delete all duplicate record except one record from the table?
Above shown through each & every method you can delete all duplicates except one record which is your base or original record.
How to remove duplicate data in Productions?
In production environment you can’t remove duplicates as I have shown ” Using Backup table ” because, in this method you have to drop the existing table but in production environment if you drop the original table then your application on which you are working it will get crushed.
What is indexing in python: In Python Indexing is the process of extracting individual elements from a given collection.
Whenever there is a data stored in memory they have certain address where they stored and they have some positions upon memory called index positions. Generally the positions starts with 0 up to total number of elements minus 1. Simply 0 to length( variable-1) .
Table of Contents
Let’s understand with an example
suppose the above is a string “hello python”, which we are storing in a variable called as str=”hello python”
So the length of this variable is 12 ( including space) and its index position started from 0 & up to 11.
So if we need to extract any element from it we need to mentions its index position.
Now if need to extract character p, we need to write code like below…
str='hello python'
str[6]
#will get output like this
'p'
What is Indexing in Python in Hindi video
Types of Indexing in Python
There are two types of index positions
Positive Index Position
Negative Index Position
Positive Indexingin Python
In positive indexing system, positive index positions get used where direction of reading elements will be left to right and the count will be start from 0.
From the above string , if I access ‘t’ I have write code like this
str='hello python'
str[8]
't' #Output
Negative Indexingin Python
In negative indexing system, negative index positions get used where direction of reading elements will be right to left and the count will be start from -1 and up to -(length of data or variable).
From the above string , if I need to access ‘t‘ i have to write code like below code
Variable is the name given to the memory allocation. This is the simplest definition of variable ever.
Table of Contents
What is variable in Python?
It is not like that the definition of variable will be changed in language to language. Whether it’s Python or C or Java, definition of variable remains same that this is the name given to the memory allocation, the value of variable can be changed based on programmers or users requirements.
On moving down to the article you will get to see how to assign the variable in python and more related topic as well.
What is variable in python in Hindi?
Here below I’m embedding one video for the reference
How to assign variable in python?
In the above image variable name is X and its value is 10. So here observe one thing that the assigned value is in right side & the variable is in left.
In python variable direction is always right to left.
That’s why Python is dynamically typed language: In Python, it doesn’t required to declare the types of variable during defining of variable.
The python interpreter automatically defines the type of variable at runtime.
Table of Contents
What does dynamically typed mean?
Whenever we define a variable we need to declare that variable’s data type so that compiler can understand what type of data stored in that variable. Without being understanding variable’s data types compiler can’t be able to any operations with this variable and resulting program will be running failed we will not get our desired output.
In some programming languages it doesn’t required to declare the data type at time of defining variable because the interpreter automatically detect the type of variable at run time, this is dynamically typed and those languages support that type are called Dynamically Typed Language.
Those languages executed directly line by line without being compiled by compiler are called interpreted language
What does strongly typed mean?
A strongly typed language requires that every variable or expression be associated with a specific data type, and that the operations performed on those variables or expressions be compatible with their data types.
What is variable declaration:
In programming, variable declaration is a core part of programming where programmer defines the variables for storing data temporarily. The values of that variable can be changed. So here in some programming languages we have to declare the type of variable also which means we need to specify what type of value we are going to store in it.
But in Python its no need to specify data type at the time of variable declaration. In below image you can see the example
Different Types of data in programming
There are so many data types ( types of value ) in programming it depends upon programming languages includes:
String
Integer
Float
Complex
List
Set
Tuple
Range
Dictionary
Boolean, etc etc.
Why Python is Dynamically Typed Languagein Hindi?
If you need more content on different concepts of python in hindi, kindly visit my youtube channel Python by Asad and do subscribe it. In this channel I am explaining all the topics in hindi.
That is why Python is interpreted language: Python uses interpreter instead of compiler to compile and run its code so that output will be efficient .
Table of Contents
What is interpreted language?
Interpreted language is a language in which the implementation and execution of instructions executes directly without compiling a program into machine language by the interpreter.
What is python in hindi simply means definition of Python language in hindi.
In this article I am going to write a simple definition of python programming language in English but later in the article a Youtube video will be embedded which is in hindi.
Table of Contents
What is python? Definition.
Python is an open source dynamically typedinterpreted high level scripting language. It accepts both functional and object oriented programming approach.
Definitely yes, Python’s object-oriented programming (OOP) features are a fundamental part of the language. Python’s syntax is simple and straightforward, which makes it easy for beginners to understand and learn.
Overall, learning OOP in Python can be a relatively simple and straightforward process for those who are new to programming.
More on Python OOPS :
Is python oops easy to learn ? More explained.
Python is known for its simple and easy-to-learn syntax, making it a great language for beginners to start with. Its object-oriented programming (OOP) features are also relatively easy to understand and use. Additionally, Python has a large and active community, which means there are many resources available for learning OOP in Python, including tutorials, documentation, and forums. Overall, many people find that Python’s OOP features are easy to learn and use, making it a good choice for beginners who are new to programming.
How python oops is different from java ?
Python and Java are both object-oriented programming languages, but they have some differences in their implementation of OOP.
Syntax: Python has a more simple and readable syntax compared to Java. Python’s syntax is designed to be easy to understand, and it requires less boilerplate code than Java.
Dynamic Typing: Python is a dynamically-typed language, meaning that the type of a variable is determined at runtime. Java, on the other hand, is a strongly-typed language, which means that the type of a variable must be defined when it is declared.
Inheritance: Both Java and Python support single inheritance, but Python also supports multiple inheritance and mixins, which Java does not.
Access Modifiers: Java has strict access modifiers (public, private, protected) while Python has less strict access modifiers ( public, protected, _private).
Exception Handling: Java and Python both have built-in support for exception handling, but the syntax is different. Java requires the use of try-catch blocks, while Python uses the raise and except keywords.
How can I learn python oops in 1 month ?
The main concept (6 nos. ) of Object Oriented Programming (OOPs) are as follows-
Class
Methods
Variables
Object
State
Behavior
Identity
Inheritance
Single Inheritance
Multi-level Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Polymorphism
Compile-time polymorphism (method overloading)
Run-time polymorphism (method overriding)
Encapsulation
Data member encapsulation
Method encapsulation
Class encapsulation
Abstraction
Data abstraction
Process abstraction
Week 1 : Class & Object ( Learn complete )
Week 2 : Inheritance
Week 3 : Polymorphism
Week 4 : Encapsulation & Abstraction
After learning python oops can I get job ?
Of course, after learning and making a strong basics of python language along with OOPs concepts, there are lots of companies they are constantly hiring python programmer who has a strong knowledge of Object oriented programming by providing a most valued package.
Is it difficult to learn python OOPS in 1 month ?
No, you can learn python oops concepts easily within a month.
How much python developers using OOPS in industry ?
In industry or tech firms those who are python developer or working in this domain are absolutely using python’s every concept but not only constantly using these. It changes based on their projects requirements or task they got assigned.
Why use OOP in python ?
Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. OOP in Python allows for the creation of reusable and modular code, as well as the ability to model real-world objects and their relationships in the program. Additionally, OOP in Python provides the ability to encapsulate and hide implementation details, making the code more maintainable and less prone to errors. Overall, OOP in Python can make the development process more efficient and organized.
Easy to learn python OOPS free resources :
Geek For Geeks (” This not promotionalrecommendation, Personally I learned lots of things ).
FAQ
How much python developer can earn monthly in india ?
Python Developer salary in India ranges between ₹ 1.9 Lakhs to ₹ 9.3 Lakhs with an average annual salary of ₹ 4.2 Lakhs. ( source )
What are the scopes after learning python in 2023 ?