YOUR AD GOES HERE

MySQL Tutorial: How to Anonymize Data Using SQL?

Published 15, Jul 2025

Database Dive


Description:
Imagine that we have a table with millions of rows.

This full SQL tutorial will tell you that to anonymize your data, you must understand the concepts of concatenating, returning elements at a specified index, flooring and randomizing, and extracting substrings from another string before or after specified delimiters:

1) According to this MySQL course, CONCAT is used to combine (concatenate) two or more strings into a single string.
2) According to this SQL full course, ELT returns the element from a list of strings at a specified index.
3) According to this SQL full course, FLOOR returns the largest number that is less than or equal to a given number.
4) According to this MySQL course, RAND generates a random number between 0 and 1.
5) Finally, this SQL course will tell you that SUBSTRING_INDEX extracts substrings from strings before or after specified delimiters.

After we understand these database concepts, we have to understand that we anonymize data by updating existing data. For that, we utilize the UPDATE SQL query.
In this SQL course, you will learn that the following SQL query would update usernames:
UPDATE `users` SET `username` = ELT(FLOOR(RAND()*3) +1), 'Username', 'Username', 'Username', ...);

This full MySQL course will tell you that a similar SQL query could be used to update a list of IP addresses:
UPDATE `users` SET `ip` = ELT(FLOOR(RAND()*3) +1), '127.0.0.1', '127.0.0.2', '127.0.0.3', ...);

These kinds of SQL queries are likely to be a part of a data analyst interview as SQL interview questions and answers for data analyst.

In this case, UPDATE specifies that we want to update data, SET sets the values for a specific column (username or IP), ELT returns the element from a list of strings at a specified index (necessary to finish our SQL UPDATE procedure), and FLOOR() and RAND() are used to generate random numbers.

A full sql course for data analyst is likely to tell you that to update values in email columns, we can utilize an SQL query like so:
UPDATE `users` SET `email` = CONCAT(ELT(FLOOR(1 + RAND() * 5), 'user1', 'user2', 'user3', 'user4', 'user5', ..., '@', SUBSTRING_INDEX(email,'@', -1));

This SQL query would tell our database that we want to SQL UPDATE our users table and set all of the values in the email column to one of the 5 values we provide. Values will be set in a random fashion. To increase or decrease the amount of values specified, fiddle around with the number that you multiply the SQL RAND() function by. In this case it's 5 because we specify five values to use after an '@' sign.
SUBSTRING_INDEX is also at play and there, we specify the column we want to update data in, the character before or after we want to find the data that needs to be updated (in this case it's "@"), then specify whether we want to update values before the character (positive values) or after the character (negative values.)

Execute those SQL queries and you will quickly see that your usernames, IP addresses, email addresses, and/or other data classes are now anonymized.

Subscribe to this database channel to see a full SQL course, obtain SQL interview questions and answers, and see other database tutorials, and until next time!

#coding #programming #database #sql #mysql #mariadb #webdevelopment

Releted More Videos

You May Also Like

YOUR AD GOES HERE

YOUR AD GOES HERE