CREATE TABLE `products` (
  `ProductID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `ProductName` varchar(40) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `SupplierID` int(10) unsigned NOT NULL,
  `CategoryID` tinyint(5) unsigned NOT NULL,
  `QuantityPerUnit` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `UnitPrice` double NOT NULL DEFAULT '0',
  `UnitsInStock` smallint(5) unsigned NOT NULL DEFAULT '0',
  `UnitsOnOrder` smallint(5) unsigned NOT NULL DEFAULT '0',
  `ReorderLevel` smallint(5) unsigned NOT NULL DEFAULT '0',
  `Discontinued` enum('y','n') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'n',
  PRIMARY KEY (`ProductID`),
  KEY `idx_FK_products_categoryid` (`CategoryID`),
  KEY `idx_FK_products_supplierid` (`SupplierID`),
  KEY `idx_products_product_name` (`ProductName`));
  
--   -- insert  into `products`(`ProductID`,`ProductName`,`SupplierID`,`CategoryID`,`QuantityPerUnit`,`UnitPrice`,
--   -- `UnitsInStock`,`UnitsOnOrder`,`ReorderLevel`,`Discontinued`) values 
--   -- (1,'Chai',1,1,'10 boxes x 20 bags',18,39,0,10,'n'),
--   -- (2,'Chang',1,1,'24 - 12 oz bottles',19,17,40,25,'n'),
--   -- (3,'Aniseed Syrup',1,2,'12 - 550 ml bottles',10,13,70,25,'n'),
--   -- (4,'Chef Anton\'s Cajun Seasoning',2,2,'48 - 6 oz jars',22,53,0,0,'n'),
--   -- (5,'Chef Anton\'s Gumbo Mix',2,2,'36 boxes',21.35,0,0,0,'y'),
--   -- (6,'Grandma\'s Boysenberry Spread',3,2,'12 - 8 oz jars',25,120,0,25,'n'),
--   -- (7,'Uncle Bob\'s Organic Dried Pears',3,7,'12 - 1 lb pkgs.',30,15,0,10,'n'),
--   -- (8,'Northwoods Cranberry Sauce',3,2,'12 - 12 oz jars',40,6,0,0,'n'),
--   -- (9,'Mishi Kobe Niku',4,6,'18 - 500 g pkgs.',97,29,0,0,'y'),
--   -- (10,'Ikura',4,8,'12 - 200 ml jars',31,31,0,0,'n'),
--   -- (11,'Queso Cabrales',5,4,'1 kg pkg.',21,22,30,30,'n'),
--   -- (12,'Queso Manchego La Pastora',5,4,'10 - 500 g pkgs.',38,86,0,0,'n'),
--   -- (13,'Konbu',6,8,'2 kg box',6,24,0,5,'n'),
--   (14,'Tofu',6,7,'40 - 100 g pkgs.',23.25,35,0,0,'n'),
--   (15,'Genen Shouyu',6,2,'24 - 250 ml bottles',15.5,39,0,5,'n'),
--   (16,'Pavlova',7,3,'32 - 500 g boxes',17.45,29,0,10,'n'),
--   (17,'Alice Mutton',7,6,'20 - 1 kg tins',39,0,0,0,'y'),
--   (18,'Carnarvon Tigers',7,8,'16 kg pkg.',62.5,42,0,0,'n'),
--   (19,'Teatime Chocolate Biscuits',8,3,'10 boxes x 12 pieces',9.2,25,0,5,'n'),
--   (20,'Sir Rodney\'s Marmalade',8,3,'30 gift boxes',81,40,0,0,'n'),
--   (21,'Sir Rodney\'s Scones',8,3,'24 pkgs. x 4 pieces',10,3,40,5,'n'),
--   (22,'Gustaf\'s Kn�ckebr�d',9,5,'24 - 500 g pkgs.',21,104,0,25,'n'),
--   (23,'Tunnbr�d',9,5,'12 - 250 g pkgs.',9,61,0,25,'n'),
--   (24,'Guaran� Fant�stica',10,1,'12 - 355 ml cans',4.5,20,0,0,'y'),
--   (25,'NuNuCa Nu�-Nougat-Creme',11,3,'20 - 450 g glasses',14,76,0,30,'n'),
--   (26,'Gumb�r Gummib�rchen',11,3,'100 - 250 g bags',31.23,15,0,0,'n'),
--   (27,'Schoggi Schokolade',11,3,'100 - 100 g pieces',43.9,49,0,30,'n'),
--   (28,'R�ssle Sauerkraut',12,7,'25 - 825 g cans',45.6,26,0,0,'y'),
--   (29,'Th�ringer Rostbratwurst',12,6,'50 bags x 30 sausgs.',123.79,0,0,0,'y'),
--   (30,'Nord-Ost Matjeshering',13,8,'10 - 200 g glasses',25.89,10,0,15,'n')
--   ,(31,'Gorgonzola Telino',14,4,'12 - 100 g pkgs',12.5,0,70,20,'n')
--   ,(32,'Mascarpone Fabioli',14,4,'24 - 200 g pkgs.',32,9,40,25,'n'),
--   (33,'Geitost',15,4,'500 g',2.5,112,0,20,'n'),
--   (34,'Sasquatch Ale',16,1,'24 - 12 oz bottles',14,111,0,15,'n'),
--   (35,'Steeleye Stout',16,1,'24 - 12 oz bottles',18,20,0,15,'n'),
--   (36,'Inlagd Sill',17,8,'24 - 250 g  jars',19,112,0,20,'n')
--   ,(37,'Gravad lax',17,8,'12 - 500 g pkgs.',26,11,50,25,'n'),
--   (38,'C�te de Blaye',18,1,'12 - 75 cl bottles',263.5,17,0,15,'n'),
--   (39,'Chartreuse verte',18,1,'750 cc per bottle',18,69,0,5,'n')
--   ,(40,'Boston Crab Meat',19,8,'24 - 4 oz tins',18.4,123,0,30,'n')
--   ,(41,'Jack\'s New England Clam Chowder',19,8,'12 - 12 oz cans',9.65,85,0,10,'n')
--   ,(42,'Singaporean Hokkien Fried Mee',20,5,'32 - 1 kg pkgs.',14,26,0,0,'y')
--   ,(43,'Ipoh Coffee',20,1,'16 - 500 g tins',46,17,10,25,'n')
--   ,(44,'Gula Malacca',20,2,'20 - 2 kg bags',19.45,27,0,15,'n')
--   ,(45,'R�gede sild',21,8,'1k pkg.',9.5,5,70,15,'n'),
--   (46,'Spegesild',21,8,'4 - 450 g glasses',12,95,0,0,'n')
--   ,(47,'Zaanse koeken',22,3,'10 - 4 oz boxes',9.5,36,0,0,'n')
--   ,(48,'Chocolade',22,3,'10 pkgs.',12.75,15,70,25,'n'),
--   (49,'Maxilaku',23,3,'24 - 50 g pkgs.',20,10,60,15,'n'),
--   (50,'Valkoinen suklaa',23,3,'12 - 100 g bars',16.25,65,0,30,'n'),
--   (51,'Manjimup Dried Apples',24,7,'50 - 300 g pkgs.',53,20,0,10,'n'),
--   (52,'Filo Mix',24,5,'16 - 2 kg boxes',7,38,0,25,'n'),
--   (53,'Perth Pasties',24,6,'48 pieces',32.8,0,0,0,'y'),
--   (54,'Tourti�re',25,6,'16 pies',7.45,21,0,10,'n'),
--   (55,'P�t� chinois',25,6,'24 boxes x 2 pies',24,115,0,20,'n'),
--   (56,'Gnocchi di nonna Alice',26,5,'24 - 250 g pkgs.',38,21,10,30,'n')
--   ,(57,'Ravioli Angelo',26,5,'24 - 250 g pkgs.',19.5,36,0,20,'n')
--   ,(58,'Escargots de Bourgogne',27,8,'24 pieces',13.25,62,0,20,'n')
--   ,(59,'Raclette Courdavault',28,4,'5 kg pkg.',55,79,0,0,'n')
--   ,(60,'Camembert Pierrot',28,4,'15 - 300 g rounds',34,19,0,0,'n')
--   ,(61,'Sirop d\'�rable',29,2,'24 - 500 ml bottles',28.5,113,0,25,'n'),
--   (62,'Tarte au sucre',29,3,'48 pies',49.3,17,0,0,'n')
--   ,(63,'Vegie-spread',7,2,'15 - 625 g jars',43.9,24,0,5,'n')
--   ,(64,'Wimmers gute Semmelkn�del',12,5,'20 bags x 4 pieces',33.25,22,80,30,'n'),
--   (65,'Louisiana Fiery Hot Pepper Sauce',2,2,'32 - 8 oz bottles',21.05,76,0,0,'n')
--   ,(66,'Louisiana Hot Spiced Okra',2,2,'24 - 8 oz jars',17,4,100,20,'n')
--   ,(67,'Laughing Lumberjack Lager',16,1,'24 - 12 oz bottles',14,52,0,10,'n'),
--   (68,'Scottish Longbreads',8,3,'10 boxes x 8 pieces',12.5,6,10,15,'n'),
--   (69,'Gudbrandsdalsost',15,4,'10 kg pkg.',36,26,0,15,'n'),
--   (70,'Outback Lager',7,1,'24 - 355 ml bottles',15,15,10,30,'n'),
--   (71,'Fl�temysost',15,4,'10 - 500 g pkgs.',21.5,26,0,0,'n'),
--   (72,'Mozzarella di Giovanni',14,4,'24 - 200 g pkgs.',34.8,14,0,0,'n'),
--   (73,'R�d Kaviar',17,8,'24 - 150 g jars',15,101,0,5,'n'),
--   (74,'Longlife Tofu',4,7,'5 kg pkg.',10,4,20,5,'n')
--   ,(75,'Rh�nbr�u Klosterbier',12,1,'24 - 0.5 l bottles',7.75,125,0,25,'n'),(
--   76,'Lakkalik��ri',23,1,'500 ml',18,57,0,20,'n'),(
--   77,'Original Frankfurter gr�ne So�e',12,2,'12 boxes',13,32,0,15,'n');

-- delete from products;


insert  into `products`(`ProductID`,`ProductName`,`SupplierID`,`CategoryID`,`QuantityPerUnit`,`UnitPrice`,
  `UnitsInStock`,`UnitsOnOrder`,`ReorderLevel`,`Discontinued`) values 
  (1,'Chai',1,1,'10 boxes x 20 bags',18,39,0,10,'n'),
  (2,'Chang',1,1,'24 - 12 oz bottles',19,17,40,25,'n'),
  (3,'Aniseed Syrup',1,2,'12 - 550 ml bottles',10,13,70,25,'n'),
  (4,'Chef Anton\'s Cajun Seasoning',2,2,'48 - 6 oz jars',22,53,0,0,'n'),
  (5,'Chef Anton\'s Gumbo Mix',2,2,'36 boxes',21.35,0,0,0,'y'),
  (6,'Grandma\'s Boysenberry Spread',3,2,'12 - 8 oz jars',25,120,0,25,'n'),
  (7,'Uncle Bob\'s Organic Dried Pears',3,7,'12 - 1 lb pkgs.',30,15,0,10,'n'),
  (8,'Northwoods Cranberry Sauce',3,2,'12 - 12 oz jars',40,6,0,0,'n'),
  (9,'Mishi Kobe Niku',4,6,'18 - 500 g pkgs.',97,29,0,0,'y'),
  (10,'Ikura',4,8,'12 - 200 ml jars',31,31,0,0,'n'),
  (11,'Queso Cabrales',5,4,'1 kg pkg.',21,22,30,30,'n'),
  (12,'Queso Manchego La Pastora',5,4,'10 - 500 g pkgs.',38,86,0,0,'n'),
  (13,'Konbu',6,8,'2 kg box',6,24,0,5,'n'),
  (14,'Tofu',6,7,'40 - 100 g pkgs.',23.25,35,0,0,'n'),
  (15,'Genen Shouyu',6,2,'24 - 250 ml bottles',15.5,39,0,5,'n'),
  (16,'Pavlova',7,3,'32 - 500 g boxes',17.45,29,0,10,'n'),
  (17,'Alice Mutton',7,6,'20 - 1 kg tins',39,0,0,0,'y'),
  (18,'Carnarvon Tigers',7,8,'16 kg pkg.',62.5,42,0,0,'n'),
  (19,'Teatime Chocolate Biscuits',8,3,'10 boxes x 12 pieces',9.2,25,0,5,'n'),
  (20,'Sir Rodney\'s Marmalade',8,3,'30 gift boxes',81,40,0,0,'n');
  
  select * from products;
  
--   Query_1 Write a query to get a Product list (id, name, unit price) where current products cost
--          less than $20.

select ProductID,ProductName,UnitPrice from products where UnitPrice>20;
  
-- Query_2 Write a query to get Product list (id, name, unit price) where products cost between
--         $15 and $25

-- select ProductID,ProductName,UnitPrice from products where UnitPrice>15 and UnitPrice<20;

-- second way

select ProductID,ProductName,UnitPrice from products where UnitPrice between 15 and 20;

-- Query_3 Write a query to get Product list (name, unit price) of above average price.

select ProductName,UnitPrice from products where unitprice > (select avg(unitprice) from products);

-- Query_4 Write a query to get Product list (name, unit price) of ten most expensive products

select ProductName,UnitPrice from products order by UnitPrice desc limit 10;

-- Query_5 Write a query to count current and discontinued products

select count(productname) from products group by discontinued;

-- Query_6 Write a query to get Product list (name, units on order , units in stock) of stock is less
--         than the quantity on order

select ProductName,UnitsOnOrder,UnitsInStock from products where UnitsInStock < UnitsOnOrder;

select max(UnitPrice),CategoryID from products group by CategoryID;

select max(UnitPrice),distinct CategoryID, ProductName from products group by CategoryID,ProductName;

select ProductName,CategoryID,max(UnitPrice) from products order by CategoryID desc

select first(ProductName),max(UnitPrice),CategoryID group by CategoryID;

select first(productname),categoryID,max(unitprice) products group by categoryID;









  
  
  
  
  
  
  
  
  
  
   

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;