create table Authors(
authorId int(10) primary key,
AuthorName varchar(255), 
ResidentCity varchar(255),
ContactNo varchar(255));
insert into Authors values(001,'Santosh','Solapur','9999988888'),(002,'Sushma','Osmanabad','9000087654'),
(003,'Sharvari','Pune','9100087654'),(004,'Pravin','Mumbai','8000087654'),(005,'Ganesh','Sangli','7500087654'),(006,'Dhruv','Kolkata','8052045210'),(007,'Alok','Pune','7582109654'),
(008,'Sonali','Goa','7500012354'),(009,'Suraj','Thane','8800087654'),(010,'Kirti','Kohapur','8075091000');

create table books(
bookId varchar(255) primary key,
authorId int(10),
BookName varchar(255),
PubicationYear int(255),
FOREIGN KEY (authorId) REFERENCES Authors(authorId));
insert into books values('kt400',004,'SQL',1999),('kt200',002,'API',2005),('kt100',001,'Selenium',2000),('kt300',003,'Manual',1985),
('kt101',001,'Automation',1995),('kt500',005,'PerformanceTesting',2010),('kt201',002,'RestAssured',2015),('kt401',004,'Database',1970),
('kt301',003,'UI-Manual',1980),('kt501',005,'SecurityTesting',2020),('kt102',001,'Jenkins',2012),('kt103',001,'GitHub',2007),
('kt600',006,'AdvancedSQL',2001),('kt700',007,'SOAPUI',2005),('kt800',008,'Appium',2002),('kt900',009,'MobileTesting',2005),
('kt110',010,'Python',1995);


create table sales(
bookId varchar(255),
OutletCityName varchar(255),
NoOfCopiesSold int(255),
FOREIGN KEY (bookId) REFERENCES books(bookId));

insert into sales values('kt400','Sangli',0),('kt400','Solapur',20),('kt400','Pune',100),('kt400','Latur',50),('kt400','Kolhapur',5),
('kt200','Beed',1),('kt200','Osmanabad',10),('kt200','Parbhani',40),('kt200','Thane',70),('kt200','Raigad',200),
('kt100','Osmanabad',0),('kt100','Aurangabad',1),('kt100','Amaravati',15),('kt100','Pune',200),('kt100','NaviMumbai',300),
('kt300','Pune',500),('kt300','Satara',50),('kt300','Mumbai',100),('kt300','Karad',40),('kt300','Solapur',150),
('kt101','Sangli',11),('kt101','Miraj',50),('kt101','Pune',50),('kt101','Solapur',80),('kt101','Pandharpur',5),
('kt500','Sangli',40),('kt500','Satara',4),('kt500','Barshi',2),('kt500','Solapur',20),('kt500','Karad',25),
('kt201','Pune',50),('kt201','Satara',5),('kt201','Mumbai',10),('kt201','Karad',400),('kt201','Solapur',10),
('kt401','Sangli',1),('kt401','Miraj',5),('kt401','Pune',3),('kt401','Solapur',8),('kt401','Pandharpur',12),
('kt301','Sangli',75),('kt301','Satara',40),('kt301','Barshi',20),('kt301','Solapur',205),('kt301','Karad',250),
('kt501','Beed',1),('kt501','Osmanabad',10),('kt501','Parbhani',40),('kt501','Thane',70),('kt501','Raigad',200),
('kt102','Osmanabad',15),('kt102','Aurangabad',1),('kt102','Amaravati',15),('kt103','Pune',150);





select * from Authors;
select * from books;
select * from sales;

select books.* from Authors inner join books
on Authors.authorId=books.authorId;

select  count(*) from(
 select  a.AuthorName,b.PubicationYear from Authors a inner join books b
 on a.authorId=b.authorId where PubicationYear>'2005')  ;
 

 --select b.BookName,s.NoOfCopiesSold from books b inner join sales s on b.bookId=s.bookId;
--Q1. Write an SQL query to print details of the books along with No of copies sold
--from every outlet
--Q3.  Write an SQL query to print book details having zero sale or no sale record

 --select BookName,NoOfCopiesSold from books left join sales
-- on books.bookId=sales.bookId where NoOfCopiesSold='0' ;


--Q2. Write an SQL query to print BookName & it's publication Year of every author.



 --number of books published by every Author in or before 2005
 

 
 
-- details of Outlet wise sales data of books published by every Author after 2005.




 
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;