├── README.md
├── q1.sql
├── q10.sql
├── q11.sql
├── q12.sql
├── q13.sql
├── q14.sql
├── q15.sql
├── q2.sql
├── q3.sql
├── q4.sql
├── q5.sql
├── q6.sql
├── q7.sql
├── q8.sql
└── q9.sql
/README.md:
--------------------------------------------------------------------------------
1 | # SQL-Chronicle
2 | A collection of SQL code solutions for beginners
3 |
4 | ## Made with 💙 by Mainak Chaudhuri
5 |
6 | 
7 |
8 |
9 | ## List of SQL keywords and their descriptions
10 |
11 | | KEYWORDS | DESCRIPTIONS |
12 | | ------------------- | ---------------|
13 | | ADD | Adds a column in an existing table |
14 | | ADD CONSTRAINT | Adds a constraint after a table is already created |
15 | | ALTER | Adds, deletes, or modifies columns in a table, or changes the data type of a column in a table |
16 | | ALTER COLUMN | Changes the data type of a column in a table |
17 | | ALTER TABLE | Adds, deletes, or modifies columns in a table |
18 | | ALL | Returns true if all of the subquery values meet the condition |
19 | | AND | Only includes rows where both conditions is true |
20 | | ANY | Returns true if any of the subquery values meet the condition |
21 | | AS | Renames a column or table with an alias |
22 | | ASC | Sorts the result set in ascending order |
23 | | BACKUP DATABASE | Creates a back up of an existing database |
24 | | BETWEEN | Selects values within a given range |
25 | | CASE | Creates different outputs based on conditions |
26 | | CHECK | A constraint that limits the value that can be placed in a column |
27 | | COLUMN | Changes the data type of a column or deletes a column in a table |
28 | | CONSTRAINT | Adds or deletes a constraint |
29 | | CREATE | Creates a database, index, view, table, or procedure |
30 | | CREATE DATABASE | Creates a new SQL database |
31 | | CREATE INDEX | Creates an index on a table (allows duplicate values) |
32 | | CREATE OR REPLACE VIEW | Updates a view |
33 | | CREATE TABLE | Creates a new table in the database |
34 | | CREATE PROCEDURE | Creates a stored procedure |
35 | | CREATE UNIQUE INDEX | Creates a unique index on a table (no duplicate values) |
36 | | CREATE VIEW | Creates a view based on the result set of a SELECT statement |
37 | | DATABASE | Creates or deletes an SQL database |
38 | | DEFAULT | A constraint that provides a default value for a column |
39 | | DELETE | Deletes rows from a table |
40 | | DESC | Sorts the result set in descending order |
41 | | DISTINCT | Selects only distinct (different) values |
42 | | DROP | Deletes a column, constraint, database, index, table, or view |
43 | | DROP COLUMN | Deletes a column in a table |
44 | | DROP CONSTRAINT | Deletes a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK constraint |
45 | | DROP DATABASE | Deletes an existing SQL database |
46 | | DROP DEFAULT | Deletes a DEFAULT constraint |
47 | | DROP INDEX | Deletes an index in a table |
48 | | DROP TABLE | Deletes an existing table in the database |
49 | | DROP VIEW | Deletes a view |
50 | | EXEC | Executes a stored procedure |
51 | | EXISTS | Tests for the existence of any record in a subquery |
52 | | FOREIGN KEY | A constraint that is a key used to link two tables together |
53 | | FROM | Specifies which table to select or delete data from |
54 | | FULL OUTER JOIN | Returns all rows when there is a match in either left table or right table |
55 | | GROUP BY | Groups the result set (used with aggregate functions: COUNT, MAX, MIN, SUM, AVG) |
56 | | HAVING | Used instead of WHERE with aggregate functions |
57 | | IN | Allows you to specify multiple values in a WHERE clause |
58 | | INDEX | Creates or deletes an index in a table |
59 | | INNER JOIN | Returns rows that have matching values in both tables |
60 | | INSERT INTO | Inserts new rows in a table |
61 | | INSERT INTO SELECT | Copies data from one table into another table |
62 | | IS NULL | Tests for empty values |
63 | | IS NOT NULL | Tests for non-empty values |
64 | | JOIN | Joins tables |
65 | | LEFT JOIN | Returns all rows from the left table, and the matching rows from the right table |
66 | | LIKE | Searches for a specified pattern in a column |
67 | | LIMIT | Specifies the number of records to return in the result set |
68 | | NOT | Only includes rows where a condition is not true |
69 | | NOT NULL | A constraint that enforces a column to not accept NULL values |
70 | | OR | Includes rows where either condition is true |
71 | | ORDER BY | Sorts the result set in ascending or descending order |
72 | | OUTER JOIN | Returns all rows when there is a match in either left table or right table |
73 | | PRIMARY KEY | A constraint that uniquely identifies each record in a database table |
74 | | PROCEDURE | A stored procedure |
75 | | RIGHT JOIN | Returns all rows from the right table, and the matching rows from the left table |
76 | | ROWNUM | Specifies the number of records to return in the result set |
77 | | SELECT | Selects data from a database |
78 | | SELECT DISTINCT | Selects only distinct (different) values |
79 | | SELECT INTO | Copies data from one table into a new table |
80 | | SELECT TOP | Specifies the number of records to return in the result set |
81 | | SET | Specifies which columns and values that should be updated in a table |
82 | | TABLE | Creates a table, or adds, deletes, or modifies columns in a table, or deletes a table or data inside a table |
83 | | TOP | Specifies the number of records to return in the result set |
84 | | TRUNCATE TABLE | Deletes the data inside a table, but not the table itself |
85 | | UNION | Combines the result set of two or more SELECT statements (only distinct values) |
86 | | UNION ALL | Combines the result set of two or more SELECT statements (allows duplicate values) |
87 | | UNIQUE | A constraint that ensures that all values in a column are unique |
88 | | UPDATE | Updates existing rows in a table |
89 | | VALUES | Specifies the values of an INSERT INTO statement |
90 | | VIEW | Creates, updates, or deletes a view |
91 | | WHERE | Filters a result set to include only records that fulfill a specified condition |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/q1.sql:
--------------------------------------------------------------------------------
1 | /*Write a query to display a string*/
2 |
3 | SELECT "This is MAINAK. I am a SQL expert";
--------------------------------------------------------------------------------
/q10.sql:
--------------------------------------------------------------------------------
1 | /*
2 | ord_no purch_amt ord_date customer_id salesman_id
3 | ---------- ---------- ---------- ----------- -----------
4 | 70001 150.5 2012-10-05 3005 5002
5 | 70009 270.65 2012-09-10 3001 5005
6 | 70002 65.26 2012-10-05 3002 5001
7 | 70004 110.5 2012-08-17 3009 5003
8 | 70007 948.5 2012-09-10 3005 5002
9 | 70005 2400.6 2012-07-27 3007 5001
10 | 70008 5760 2012-09-10 3002 5001
11 | 70010 1983.43 2012-10-10 3004 5006
12 | 70003 2480.4 2012-10-10 3009 5003
13 | 70012 250.45 2012-06-27 3008 5002
14 | 70011 75.29 2012-08-17 3003 5007
15 | 70013 3045.6 2012-04-25 3002 5001
16 | */
17 |
18 | -- Write a sql query to display the order number followed by order date and the purchase amount for each order which will be delivered by the salesman who is holding the ID 5001.
19 |
20 | SELECT ord_no, ord_date, purch_amt
21 | FROM orders
22 | WHERE salesman_id=5001;
--------------------------------------------------------------------------------
/q11.sql:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | YEAR SUBJECT WINNER COUNTRY CATEGORY
4 | ---- ------------------------- --------------------------------------------- ------------------------- ------------
5 | 1970 Physics Hannes Alfven Sweden Scientist
6 | 1970 Physics Louis Neel France Scientist
7 | 1970 Chemistry Luis Federico Leloir France Scientist
8 | 1970 Physiology Ulf von Euler Sweden Scientist
9 | 1970 Physiology Bernard Katz Germany Scientist
10 | 1970 Literature Aleksandr Solzhenitsyn Russia Linguist
11 | 1970 Economics Paul Samuelson USA Economist
12 | 1970 Physiology Julius Axelrod USA Scientist
13 | 1971 Physics Dennis Gabor Hungary Scientist
14 | 1971 Chemistry Gerhard Herzberg Germany Scientist
15 | 1971 Peace Willy Brandt Germany Chancellor
16 | 1971 Literature Pablo Neruda Chile Linguist
17 | 1971 Economics Simon Kuznets Russia Economist
18 | 1978 Peace Anwar al-Sadat Egypt President
19 | 1978 Peace Menachem Begin Israel Prime Minister
20 | 1987 Chemistry Donald J. Cram USA Scientist
21 | 1987 Chemistry Jean-Marie Lehn France Scientist
22 | 1987 Physiology Susumu Tonegawa Japan Scientist
23 | 1994 Economics Reinhard Selten Germany Economist
24 | 1994 Peace Yitzhak Rabin Israel Prime Minister
25 | 1987 Physics Johannes Georg Bednorz Germany Scientist
26 | 1987 Literature Joseph Brodsky Russia Linguist
27 | 1987 Economics Robert Solow USA Economist
28 | 1994 Literature Kenzaburo Oe Japan Linguist
29 | */
30 |
31 | -- Write a SQL query to display the Nobel prizes for 1970.
32 |
33 | SELECT year,subject,winner
34 | FROM nobel_win
35 | WHERE year=1970;
36 |
--------------------------------------------------------------------------------
/q12.sql:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | YEAR SUBJECT WINNER COUNTRY CATEGORY
4 | ---- ------------------------- --------------------------------------------- ------------------------- ------------
5 | 1970 Physics Hannes Alfven Sweden Scientist
6 | 1970 Physics Louis Neel France Scientist
7 | 1970 Chemistry Luis Federico Leloir France Scientist
8 | 1970 Physiology Ulf von Euler Sweden Scientist
9 | 1970 Physiology Bernard Katz Germany Scientist
10 | 1970 Literature Aleksandr Solzhenitsyn Russia Linguist
11 | 1970 Economics Paul Samuelson USA Economist
12 | 1970 Physiology Julius Axelrod USA Scientist
13 | 1971 Physics Dennis Gabor Hungary Scientist
14 | 1971 Chemistry Gerhard Herzberg Germany Scientist
15 | 1971 Peace Willy Brandt Germany Chancellor
16 | 1971 Literature Pablo Neruda Chile Linguist
17 | 1971 Economics Simon Kuznets Russia Economist
18 | 1978 Peace Anwar al-Sadat Egypt President
19 | 1978 Peace Menachem Begin Israel Prime Minister
20 | 1987 Chemistry Donald J. Cram USA Scientist
21 | 1987 Chemistry Jean-Marie Lehn France Scientist
22 | 1987 Physiology Susumu Tonegawa Japan Scientist
23 | 1994 Economics Reinhard Selten Germany Economist
24 | 1994 Peace Yitzhak Rabin Israel Prime Minister
25 | 1987 Physics Johannes Georg Bednorz Germany Scientist
26 | 1987 Literature Joseph Brodsky Russia Linguist
27 | 1987 Economics Robert Solow USA Economist
28 | 1994 Literature Kenzaburo Oe Japan Linguist
29 | */
30 |
31 | -- Write a SQL query to know the winner of the 1971 prize for Literature.
32 |
33 | SELECT winner
34 | FROM nobel_win
35 | WHERE year = 1971
36 | AND subject = 'Literature';
37 |
--------------------------------------------------------------------------------
/q13.sql:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | YEAR SUBJECT WINNER COUNTRY CATEGORY
4 | ---- ------------------------- --------------------------------------------- ------------------------- ------------
5 | 1970 Physics Hannes Alfven Sweden Scientist
6 | 1970 Physics Louis Neel France Scientist
7 | 1970 Chemistry Luis Federico Leloir France Scientist
8 | 1970 Physiology Ulf von Euler Sweden Scientist
9 | 1970 Physiology Bernard Katz Germany Scientist
10 | 1970 Literature Aleksandr Solzhenitsyn Russia Linguist
11 | 1970 Economics Paul Samuelson USA Economist
12 | 1970 Physiology Julius Axelrod USA Scientist
13 | 1971 Physics Dennis Gabor Hungary Scientist
14 | 1971 Chemistry Gerhard Herzberg Germany Scientist
15 | 1971 Peace Willy Brandt Germany Chancellor
16 | 1971 Literature Pablo Neruda Chile Linguist
17 | 1971 Economics Simon Kuznets Russia Economist
18 | 1978 Peace Anwar al-Sadat Egypt President
19 | 1978 Peace Menachem Begin Israel Prime Minister
20 | 1987 Chemistry Donald J. Cram USA Scientist
21 | 1987 Chemistry Jean-Marie Lehn France Scientist
22 | 1987 Physiology Susumu Tonegawa Japan Scientist
23 | 1994 Economics Reinhard Selten Germany Economist
24 | 1994 Peace Yitzhak Rabin Israel Prime Minister
25 | 1987 Physics Johannes Georg Bednorz Germany Scientist
26 | 1987 Literature Joseph Brodsky Russia Linguist
27 | 1987 Economics Robert Solow USA Economist
28 | 1994 Literature Kenzaburo Oe Japan Linguist
29 | */
30 |
31 | -- Write a SQL query to display the year and subject that won 'Dennis Gabor' his prize.
32 |
33 | SELECT year, subject
34 | FROM nobel_win
35 | WHERE winner = 'Dennis Gabor';
36 |
--------------------------------------------------------------------------------
/q14.sql:
--------------------------------------------------------------------------------
1 | /*
2 | YEAR SUBJECT WINNER COUNTRY CATEGORY
3 | ---- ------------------------- --------------------------------------------- ------------------------- ------------
4 | 1970 Physics Hannes Alfven Sweden Scientist
5 | 1970 Physics Louis Neel France Scientist
6 | 1970 Chemistry Luis Federico Leloir France Scientist
7 | 1970 Physiology Ulf von Euler Sweden Scientist
8 | 1970 Physiology Bernard Katz Germany Scientist
9 | 1970 Literature Aleksandr Solzhenitsyn Russia Linguist
10 | 1970 Economics Paul Samuelson USA Economist
11 | 1970 Physiology Julius Axelrod USA Scientist
12 | 1971 Physics Dennis Gabor Hungary Scientist
13 | 1971 Chemistry Gerhard Herzberg Germany Scientist
14 | 1971 Peace Willy Brandt Germany Chancellor
15 | 1971 Literature Pablo Neruda Chile Linguist
16 | 1971 Economics Simon Kuznets Russia Economist
17 | 1978 Peace Anwar al-Sadat Egypt President
18 | 1978 Peace Menachem Begin Israel Prime Minister
19 | 1987 Chemistry Donald J. Cram USA Scientist
20 | 1987 Chemistry Jean-Marie Lehn France Scientist
21 | 1987 Physiology Susumu Tonegawa Japan Scientist
22 | 1994 Economics Reinhard Selten Germany Economist
23 | 1994 Peace Yitzhak Rabin Israel Prime Minister
24 | 1987 Physics Johannes Georg Bednorz Germany Scientist
25 | 1987 Literature Joseph Brodsky Russia Linguist
26 | 1987 Economics Robert Solow USA Economist
27 | 1994 Literature Kenzaburo Oe Japan Linguist
28 |
29 | */
30 |
31 | -- Write a SQL query to give the name of the 'Physics' winners since the year 1950.
32 |
33 | SELECT winner
34 | FROM nobel_win
35 | WHERE year>=1950
36 | AND subject='Physics';
37 |
--------------------------------------------------------------------------------
/q15.sql:
--------------------------------------------------------------------------------
1 | /*
2 | YEAR SUBJECT WINNER COUNTRY CATEGORY
3 | ---- ------------------------- --------------------------------------------- ------------------------- ------------
4 | 1970 Physics Hannes Alfven Sweden Scientist
5 | 1970 Physics Louis Neel France Scientist
6 | 1970 Chemistry Luis Federico Leloir France Scientist
7 | 1970 Physiology Ulf von Euler Sweden Scientist
8 | 1970 Physiology Bernard Katz Germany Scientist
9 | 1970 Literature Aleksandr Solzhenitsyn Russia Linguist
10 | 1970 Economics Paul Samuelson USA Economist
11 | 1970 Physiology Julius Axelrod USA Scientist
12 | 1971 Physics Dennis Gabor Hungary Scientist
13 | 1971 Chemistry Gerhard Herzberg Germany Scientist
14 | 1971 Peace Willy Brandt Germany Chancellor
15 | 1971 Literature Pablo Neruda Chile Linguist
16 | 1971 Economics Simon Kuznets Russia Economist
17 | 1978 Peace Anwar al-Sadat Egypt President
18 | 1978 Peace Menachem Begin Israel Prime Minister
19 | 1987 Chemistry Donald J. Cram USA Scientist
20 | 1987 Chemistry Jean-Marie Lehn France Scientist
21 | 1987 Physiology Susumu Tonegawa Japan Scientist
22 | 1994 Economics Reinhard Selten Germany Economist
23 | 1994 Peace Yitzhak Rabin Israel Prime Minister
24 | 1987 Physics Johannes Georg Bednorz Germany Scientist
25 | 1987 Literature Joseph Brodsky Russia Linguist
26 | 1987 Economics Robert Solow USA Economist
27 | 1994 Literature Kenzaburo Oe Japan Linguist
28 |
29 | */
30 |
31 | -- Write a SQL query to Show all the details (year, subject, winner, country ) of the Chemistry prize winners between the year 1965 to 1975 inclusive.
32 |
33 | SELECT year, subject, winner, country
34 | FROM nobel_win
35 | WHERE subject = 'Chemistry'
36 | AND year>=1965 AND year<=1975;
37 |
--------------------------------------------------------------------------------
/q2.sql:
--------------------------------------------------------------------------------
1 | /* Write a query to display three numbers in sql */
2 |
3 | SELECT 5, 10, 15;
4 |
--------------------------------------------------------------------------------
/q3.sql:
--------------------------------------------------------------------------------
1 | /* Mathematical operations in SQL */
2 |
3 | SELECT 10 + 15;
4 | SELECT 20 - 10;
5 | SELECT 4 * 10;
6 | SELECT 80 / 4;
7 |
--------------------------------------------------------------------------------
/q4.sql:
--------------------------------------------------------------------------------
1 | /*salesman_id | name | city | commission
2 | -------------+------------+----------+------------
3 | 5001 | James Hoog | New York | 0.15
4 | 5002 | Nail Knite | Paris | 0.13
5 | 5005 | Pit Alex | London | 0.11
6 | 5006 | Mc Lyon | Paris | 0.14
7 | 5007 | Paul Adam | Rome | 0.13
8 | 5003 | Lauson Hen | San Jose | 0.12*/
9 |
10 | -- Display all rows of this table
11 |
12 |
13 | SELECT * FROM salesman;
--------------------------------------------------------------------------------
/q5.sql:
--------------------------------------------------------------------------------
1 | /*
2 | salesman_id | name | city | commission
3 | -------------+------------+----------+------------
4 | 5001 | James Hoog | New York | 0.15
5 | 5002 | Nail Knite | Paris | 0.13
6 | 5005 | Pit Alex | London | 0.11
7 | 5006 | Mc Lyon | Paris | 0.14
8 | 5007 | Paul Adam | Rome | 0.13
9 | 5003 | Lauson Hen | San Jose | 0.12
10 | */
11 |
12 | -- Write a query to display only name and commission from table salesman.
13 |
14 | SELECT name, commission
15 | FROM salesman;
--------------------------------------------------------------------------------
/q6.sql:
--------------------------------------------------------------------------------
1 | /*
2 | ord_no purch_amt ord_date customer_id salesman_id
3 | ---------- ---------- ---------- ----------- -----------
4 | 70001 150.5 2012-10-05 3005 5002
5 | 70009 270.65 2012-09-10 3001 5005
6 | 70002 65.26 2012-10-05 3002 5001
7 | 70004 110.5 2012-08-17 3009 5003
8 | 70007 948.5 2012-09-10 3005 5002
9 | 70005 2400.6 2012-07-27 3007 5001
10 | 70008 5760 2012-09-10 3002 5001
11 | 70010 1983.43 2012-10-10 3004 5006
12 | 70003 2480.4 2012-10-10 3009 5003
13 | 70012 250.45 2012-06-27 3008 5002
14 | 70011 75.29 2012-08-17 3003 5007
15 | 70013 3045.6 2012-04-25 3002 5001
16 | */
17 |
18 | -- Write a query to display salesman_id, salesman name, order date, order no, and purchase amount from orders table.
19 |
20 | SELECT ord_date, salesman_id, ord_no, purch_amt
21 | FROM orders;
22 |
--------------------------------------------------------------------------------
/q7.sql:
--------------------------------------------------------------------------------
1 | /*
2 | ord_no purch_amt ord_date customer_id salesman_id
3 | ---------- ---------- ---------- ----------- -----------
4 | 70001 150.5 2012-10-05 3005 5002
5 | 70009 270.65 2012-09-10 3001 5005
6 | 70002 65.26 2012-10-05 3002 5001
7 | 70004 110.5 2012-08-17 3009 5003
8 | 70007 948.5 2012-09-10 3005 5002
9 | 70005 2400.6 2012-07-27 3007 5001
10 | 70008 5760 2012-09-10 3002 5001
11 | 70010 1983.43 2012-10-10 3004 5006
12 | 70003 2480.4 2012-10-10 3009 5003
13 | 70012 250.45 2012-06-27 3008 5002
14 | 70011 75.29 2012-08-17 3003 5007
15 | 70013 3045.6 2012-04-25 3002 5001
16 | */
17 |
18 | -- Write a query which will retrieve the values of salesman id of all salesmen getting orders from the customers in orders table without any repeats.
19 |
20 | SELECT DISTINCT salesman_id
21 | FROM orders;
22 |
--------------------------------------------------------------------------------
/q8.sql:
--------------------------------------------------------------------------------
1 | /*
2 | salesman_id | name | city | commission
3 | -------------+------------+----------+------------
4 | 5001 | James Hoog | New York | 0.15
5 | 5002 | Nail Knite | Paris | 0.13
6 | 5005 | Pit Alex | London | 0.11
7 | 5006 | Mc Lyon | Paris | 0.14
8 | 5007 | Paul Adam | Rome | 0.13
9 | 5003 | Lauson Hen | San Jose | 0.12
10 | */
11 |
12 | -- Write a query to display names and city of salesman, who belongs to the city of Paris
13 |
14 | SELECT name,city
15 | FROM salesman
16 | WHERE city='Paris';
--------------------------------------------------------------------------------
/q9.sql:
--------------------------------------------------------------------------------
1 | /*
2 | customer_id | cust_name | city | grade | salesman_id
3 | -------------+----------------+------------+-------+-------------
4 | 3002 | Nick Rimando | New York | 100 | 5001
5 | 3007 | Brad Davis | New York | 200 | 5001
6 | 3005 | Graham Zusi | California | 200 | 5002
7 | 3008 | Julian Green | London | 300 | 5002
8 | 3004 | Fabian Johnson | Paris | 300 | 5006
9 | 3009 | Geoff Cameron | Berlin | 100 | 5003
10 | 3003 | Jozy Altidor | Moscow | 200 | 5007
11 | 3001 | Brad Guzan | London | | 5005
12 | */
13 |
14 | -- Write a SQL statement to display all the information for those customers with a grade of 200.
15 |
16 | SELECT *FROM customer
17 | WHERE grade=200;
--------------------------------------------------------------------------------