-- create
CREATE TABLE transactions (
    Transaction_ID INT PRIMARY KEY AUTO_INCREMENT,
    Customer_ID INT,
    Transaction_Date DATE,
    Product_ID INT,
    Product_Name VARCHAR(255),
    Unit_Price DECIMAL(10, 2),
    Quantity INT,
    Total_Price DECIMAL(10, 2),
    Country VARCHAR(255),
    Payment_Method VARCHAR(50)
);


-- insert
INSERT INTO transactions (Transaction_ID, Customer_ID, Transaction_Date, Product_ID, Product_Name, Unit_Price, Quantity, Total_Price, Country, Payment_Method) VALUES
(1, 101, '2023-01-01', 1, 'Blue T-shirt', 15.99, 2, 31.98, 'USA', 'Credit Card'),
(2, 102, '2023-01-02', 2, 'Black Jeans', 29.99, 1, 29.99, 'UK', 'PayPal'),
(3, 103, '2023-01-03', 3, 'Red Dress', 39.99, 1, 39.99, 'Canada', 'Credit Card'),
(4, 104, '2023-01-04', 4, 'White Sneakers', 24.99, 2, 49.98, 'USA', 'Debit Card'),
(5, 105, '2023-01-05', 5, 'Gray Hoodie', 34.99, 1, 34.99, 'USA', 'Credit Card'),
(6, 106, '2023-01-06', 6, 'Denim Jacket', 49.99, 1, 49.99, 'UK', 'PayPal'),
(7, 107, '2023-01-07', 7, 'Green Skirt', 19.99, 2, 39.98, 'Canada', 'Credit Card'),
(8, 108, '2023-01-08', 8, 'Purple Scarf', 9.99, 3, 29.97, 'USA', 'PayPal'),
(9, 109, '2023-01-09', 9, 'Yellow Sunglasses', 14.99, 1, 14.99, 'UK', 'Debit Card'),
(10, 110, '2023-01-10', 10, 'Orange Backpack', 44.99, 1, 44.99, 'USA', 'Credit Card'),
(11, 111, '2023-01-11', 11, 'Striped Shirt', 19.99, 2, 39.98, 'Canada', 'Debit Card'),
(12, 112, '2023-01-12', 12, 'Polka Dot Dress', 59.99, 1, 59.99, 'UK', 'Credit Card'),
(13, 113, '2023-01-13', 13, 'Plaid Shorts', 24.99, 1, 24.99, 'USA', 'PayPal'),
(14, 114, '2023-01-14', 14, 'Checked Blouse', 29.99, 1, 29.99, 'UK', 'Credit Card'),
(15, 115, '2023-01-15', 15, 'Floral Skirt', 19.99, 2, 39.98, 'Canada', 'Debit Card'),
(16, 116, '2023-01-16', 16, 'Leather Jacket', 79.99, 1, 79.99, 'USA', 'Credit Card'),
(17, 117, '2023-01-17', 17, 'Sweatpants', 34.99, 1, 34.99, 'UK', 'PayPal'),
(18, 118, '2023-01-18', 18, 'Hooded Sweatshirt', 49.99, 1, 49.99, 'Canada', 'Credit Card'),
(19, 119, '2023-01-19', 19, 'Beanie Hat', 12.99, 2, 25.98, 'USA', 'Credit Card'),
(20, 120, '2023-01-20', 20, 'Silk Scarf', 29.99, 1, 29.99, 'UK', 'Debit Card'),
(21, 121, '2023-01-21', 21, 'Cotton Socks', 9.99, 3, 29.97, 'Canada', 'PayPal'),
(22, 122, '2023-01-22', 22, 'Leather Belt', 19.99, 1, 19.99, 'USA', 'Credit Card'),
(23, 123, '2023-01-23', 23, 'Canvas Shoes', 29.99, 1, 29.99, 'UK', 'Debit Card'),
(24, 124, '2023-01-24', 24, 'Printed Tote Bag', 39.99, 1, 39.99, 'Canada', 'Credit Card'),
(25, 125, '2023-01-25', 25, 'Plaid Shirt', 24.99, 2, 49.98, 'USA', 'PayPal'),
(26, 126, '2023-01-26', 26, 'Striped Sweater', 39.99, 1, 39.99, 'UK', 'Debit Card'),
(27, 127, '2023-01-27', 27, 'Denim Shorts', 29.99, 1, 29.99, 'Canada', 'PayPal'),
(28, 128, '2023-01-28', 28, 'Lace Top', 19.99, 2, 39.98, 'USA', 'Credit Card'),
(29, 129, '2023-01-29', 29, 'Knit Beanie', 14.99, 1, 14.99, 'UK', 'Credit Card'),
(30, 130, '2023-01-30', 30, 'Plaid Scarf', 24.99, 1, 24.99, 'Canada', 'Debit Card'),
(31, 101, '2023-01-31', 1, 'Blue T-shirt', 15.99, 2, 31.98, 'USA', 'Credit Card'),
(32, 102, '2023-02-01', 2, 'Black Jeans', 29.99, 1, 29.99, 'UK', 'PayPal'),
(33, 103, '2023-02-02', 3, 'Red Dress', 39.99, 1, 39.99, 'Canada', 'Credit Card'),
(34, 104, '2023-02-03', 4, 'White Sneakers', 24.99, 2, 49.98, 'USA', 'Debit Card'),
(35, 105, '2023-02-04', 5, 'Gray Hoodie', 34.99, 1, 34.99, 'USA', 'Credit Card'),
(36, 106, '2023-02-05', 6, 'Denim Jacket', 49.99, 1, 49.99, 'UK', 'PayPal'),
(37, 107, '2023-02-06', 7, 'Green Skirt', 19.99, 2, 39.98, 'Canada', 'Credit Card'),
(38, 108, '2023-02-07', 8, 'Purple Scarf', 9.99, 3, 29.97, 'USA', 'PayPal'),
(39, 109, '2023-02-08', 9, 'Yellow Sunglasses', NULL, 1, 14.99, 'UK', 'Debit Card'),
(40, 110, '2023-02-09', 10, 'Orange Backpack', 44.99, 1, NULL, 'USA', 'Credit Card'),
(41, 111, '2023-02-10', 11, 'Striped Shirt', 19.99, 2, 39.98, 'Canada', 'Debit Card'),
(42, 112, '2023-02-11', 12, 'Polka Dot Dress', 59.99, 1, 59.99, 'UK', 'Credit Card'),
(43, 113, '2023-02-12', 13, 'Plaid Shorts', 24.99, 1, 24.99, 'USA', 'PayPal'),
(44, 114, '2023-02-13', 14, 'Checked Blouse', 29.99, 1, 29.99, 'UK', 'Credit Card'),
(45, 115, '2023-02-14', 15, 'Floral Skirt', 19.99, 2, 39.98, 'Canada', 'Debit Card'),
(46, 116, '2023-02-15', 16, 'Leather Jacket', 79.99, 1, 79.99, 'USA', 'Credit Card'),
(47, 117, '2023-02-16', 17, 'Sweatpants', 34.99, 1, 34.99, 'UK', 'PayPal'),
(48, 118, '2023-02-17', 18, 'Hooded Sweatshirt', 49.99, 1, 49.99, 'Canada', 'Credit Card'),
(49, 119, '2023-02-18', 19, 'Beanie Hat', 12.99, 2, 25.98, 'USA', 'Credit Card'),
(50, 120, '2023-02-19', 20, 'Silk Scarf', 29.99, 1, 29.99, 'UK', 'Debit Card');



-- Create customer_info table
CREATE TABLE customer_info (
    Customer_ID INT PRIMARY KEY,
    Customer_Name VARCHAR(255),
    Email VARCHAR(255)
);

-- Insert sample customer data
INSERT INTO customer_info (Customer_ID, Customer_Name, Email)
VALUES
    (101, 'John Doe', '[email protected]'),
    (102, 'Jane Smith', '[email protected]'),
    (103, 'Alice Johnson', '[email protected]');

-- Add Discount column to transactions table
ALTER TABLE transactions
ADD COLUMN Discount DECIMAL(10, 2);

-- Calculate 10% discount for each transaction
UPDATE transactions
SET Discount = Total_Price * 0.1;

-- Delete transactions with a quantity of 0 or negative values
DELETE FROM transactions
WHERE Quantity <= 0;

-- joining Tables
SELECT t.Transaction_ID, t.Customer_ID, c.Customer_Name, c.Email, t.Transaction_Date, t.Product_ID, t.Product_Name, t.Unit_Price, t.Quantity, t.Total_Price, t.Country, t.Payment_Method, t.Discount
FROM transactions t
INNER JOIN customer_info c ON t.Customer_ID = c.Customer_ID;


-- Retrieve transaction details along with corresponding customer information
SELECT 
    t.Transaction_ID,
    t.Customer_ID,
    c.Customer_Name,
    c.Email,
    t.Transaction_Date,
    t.Product_ID,
    t.Product_Name,
    t.Unit_Price,
    t.Quantity,
    t.Total_Price,
    t.Country,
    t.Payment_Method
FROM 
    transactions t
INNER JOIN 
    customer_info c ON t.Customer_ID = c.Customer_ID;

-- Calculate the total price for each transaction (Unit_Price * Quantity)
SELECT 
    Transaction_ID,
    Customer_ID,
    Transaction_Date,
    Product_ID,
    Product_Name,
    Unit_Price,
    Quantity,
    (Unit_Price * Quantity) AS Total_Price,
    Country,
    Payment_Method
FROM 
    transactions;

-- Extract the month and year from the 'Transaction_Date' column
SELECT 
    Transaction_ID,
    Customer_ID,
    Transaction_Date,
    Product_ID,
    Product_Name,
    Unit_Price,
    Quantity,
    Total_Price,
    Country,
    Payment_Method,
    MONTH(Transaction_Date) AS Transaction_Month,
    YEAR(Transaction_Date) AS Transaction_Year
FROM 
    transactions;

-- Concatenate the 'Product_Name' and 'Country' columns to create a new column 'Product_Location'
SELECT 
    Transaction_ID,
    Customer_ID,
    Transaction_Date,
    Product_ID,
    Product_Name,
    Unit_Price,
    Quantity,
    Total_Price,
    Country,
    Payment_Method,
    CONCAT(Product_Name, ', ', Country) AS Product_Location
FROM 
    transactions;


 
by

MySQL online editor

Write, Run & Share MySQL queries online using OneCompiler's MySQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for MySQL. Getting started with the OneCompiler's MySQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'MySQL' and start writing queries to learn and test online without worrying about tedious process of installation.

About MySQL

MySQL is a open-source, free and very popular relational database management system which is developed, distributed and supported by Oracle corporation.

Key Features:

  • Open-source relational database management systems.
  • Reliable, very fast and easy to use database server.
  • Works on client-server model.
  • Highly Secure and Scalable
  • High Performance
  • High productivity as it uses stored procedures, triggers, views to write a highly productive code.
  • Supports large databases efficiently.
  • Supports many operating systems like Linux*,CentOS*, Solaris*,Ubuntu*,Windows*, MacOS*,FreeBSD* and others.

Syntax help

Commands

1. CREATE

CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

Example

CREATE TABLE EMPLOYEE (
  empId INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept TEXT NOT NULL
);

2. ALTER

ALTER TABLE Table_name ADD column_name datatype;

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Dave', 'Sales');

3. TRUNCATE

TRUNCATE table table_name;

4. DROP

DROP TABLE table_name;

5. RENAME

RENAME TABLE table_name1 to new_table_name1; 

6. COMMENT

Single-Line Comments:

 --Line1;

Multi-Line comments:

   /* Line1,
   Line2 */

DML Commands

1. INSERT

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

Note: Column names are optional.

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Ava', 'Sales');

2. SELECT

SELECT column1, column2, ...
FROM table_name
[where condition]; 

Example

SELECT * FROM EMPLOYEE where dept ='sales';

3. UPDATE

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

Example

UPDATE EMPLOYEE SET dept = 'Sales' WHERE empId='0001'; 

4. DELETE

DELETE FROM table_name where condition;

Example

DELETE from EMPLOYEE where empId='0001'; 

Indexes

1. CREATE INDEX

  CREATE INDEX index_name on table_name(column_name);
  • To Create Unique index:
  CREATE UNIQUE INDEX index_name on table_name(column_name);

2. DROP INDEX

DROP INDEX index_name ON table_name;

Views

1. Create a View

Creating a View:
CREATE VIEW View_name AS 
Query;

2. How to call view

SELECT * FROM View_name;

3. Altering a View

ALTER View View_name AS 
Query;

4. Deleting a View

DROP VIEW View_name;

Triggers

1. Create a Trigger

CREATE TRIGGER trigger_name trigger_time trigger_event
    ON tbl_name FOR EACH ROW [trigger_order] trigger_body
/* where
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
trigger_order: { FOLLOWS | PRECEDES } */

2. Drop a Trigger

DROP TRIGGER [IF EXISTS] trigger_name;

Stored Procedures

1. Create a Stored Procedure

CREATE PROCEDURE sp_name(p1 datatype)
BEGIN
/*Stored procedure code*/
END;

2. How to call Stored procedure

CALL sp_name;

3. How to delete stored procedure

DROP PROCEDURE sp_name;

Joins

1. INNER JOIN

SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;

2. LEFT JOIN

SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;

3. RIGHT JOIN

SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;

4. CROSS JOIN

SELECT select_list from TABLE1 CROSS JOIN TABLE2;