Data cleaning in SQL involves identifying and correcting errors or inconsistencies in data to improve its quality and accuracy.
Returns the first non-NULL value from a list.
SELECT name, base_salary, COALESCE(bonus, 0) AS bonus FROM employees;
Replaces NULL with a specified value.
SELECT name, base_salary, IFNULL(bonus, 0) AS bonus FROM employees;
Checks if a value is NULL and replaces it.
SELECT name, base_salary, ISNULL(bonus, 0) AS bonus FROM employees;
Removes duplicate rows.
SELECT DISTINCT name, department FROM employees;
Assigns a unique row number and helps remove duplicates.
SELECT name, department, ROW_NUMBER() OVER (PARTITION BY name ORDER BY id) AS row_num FROM employees;
SELECT LOWER(name) FROM employees;
SELECT UPPER(name) FROM employees;
SELECT TRIM(name) FROM employees;
UPDATE products SET product_code = CONCAT('PROD-', SUBSTR(TRIM(product_code), INSTR(product_code, '-') + 1));
SELECT CAST(sale_id AS VARCHAR(10)) FROM sales;
SELECT CONVERT(VARCHAR(10), sale_date, 103) FROM sales;
SELECT STR_TO_DATE(order_date, '%d/%m/%Y') FROM orders;
SELECT EXTRACT(YEAR FROM order_date) FROM orders;
CREATE TABLE Customers (CustomerID INT PRIMARY KEY, Age INT, CHECK (Age >= 18 AND Age <= 100));
SELECT ROUND(sale_amount, 2) FROM sales_data;
SELECT CEIL(sale_amount) FROM sales_data;
SELECT FLOOR(sale_amount) FROM sales_data;
SELECT ABS(sale_amount) FROM sales_data;