forked from Internshala-Online-Trainings/Web-Development
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
746eff9
commit 7e4ac4c
Showing
13 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
The queries used in the lesson. | ||
|
||
CREATE database temp_db; | ||
CREATE TABLE temp_db.products ( id INT(11), name VARCHAR(30) , category INT(30) ); | ||
CREATE TABLE products ( id INT(11), name VARCHAR(30) , category INT(30) ); | ||
CREATE TABLE temp_db.products_1 ( id INT(11) NOT NULL, name VARCHAR(30) , category INT(30) ); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
The queries used in the lesson. | ||
|
||
CREATE TABLE temp_db.products_2 ( id INT NOT NULL, name VARCHAR(30), category INT(30), PRIMARY KEY (id)); | ||
CREATE TABLE temp_db.products_3 ( id INT NOT NULL PRIMARY KEY, name VARCHAR(30), category INT(30)); | ||
CREATE TABLE temp_db.products_4 ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), category INT(30)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
The queries used in the lesson. | ||
|
||
Alter table database_name.table_name CHANGE column_name new_column_name data type(length) constraints; | ||
Alter table temp_db.temp_users CHANGE first_name name varchar(255); | ||
Alter table database_name.table_name MODIFY column_name datatype properties; | ||
Alter table temp_db.temp_users MODIFY last_name varchar(255) NOT NULL; | ||
Alter table temp_db.temp_users MODIFY name char(255) NOT NULL; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
The queries used in the lesson. | ||
|
||
DROP table temp_db.products_1; | ||
DROP table temp_db.products_2; | ||
DROP table temp_db.products_3; | ||
DROP table temp_db.products_4; | ||
DROP database temp_db; |
6 changes: 6 additions & 0 deletions
6
Module 3 - SQL/Topic 05 - Alter & Drop/Table Structure Using Alter Command .txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
The queries used in the lesson. | ||
|
||
ALTER TABLE temp_db.products MODIFY id INT NOT NULL; | ||
ALTER TABLE temp_db.products ADD PRIMARY KEY(id); | ||
ALTER TABLE temp_db.products MODIFY COLUMN id INT AUTO_INCREMENT; | ||
|
5 changes: 5 additions & 0 deletions
5
Module 3 - SQL/Topic 06 - Foreign Key & Truncate/Foreign Key.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The queries used in the lesson. | ||
|
||
CREATE table products query; | ||
CREATE table users_products query; | ||
|
5 changes: 5 additions & 0 deletions
5
Module 3 - SQL/Topic 06 - Foreign Key & Truncate/Truncate.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The queries used in the lesson. | ||
|
||
TRUNCATE TABLE users_products; | ||
TRUNCATE TABLE users; | ||
|
16 changes: 16 additions & 0 deletions
16
Module 3 - SQL/Topic 07 - DML/Data Manipulation Language.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
The queries used in the lesson. | ||
|
||
INSERT INTO ecommerce.products (name, category) values ('harry_potter', 'book'); | ||
INSERT INTO ecommerce.products (name, category) values ('deception_point', 'book'); | ||
INSERT INTO ecommerce.products (name, category) values ('drunkards_walk', 'book'); | ||
INSERT INTO ecommerce.products (name, category) values ('pencil_box', 'stationery'); | ||
INSERT INTO ecommerce.products (name, category) values ('night_lamp', 'other'); | ||
|
||
|
||
INSERT INTO ecommerce.users (email, first_name, last_name, phone) values ('[email protected]', 'Max', 'Joseph', 9915174990); | ||
INSERT INTO ecommerce.users (email, first_name, last_name, phone) values ('[email protected]', 'Atif', NULL, 9915174991); | ||
INSERT INTO ecommerce.users (email, first_name, last_name, phone) values ('[email protected]', 'Amar', NULL, 9915174956); | ||
INSERT INTO ecommerce.users (email, first_name, last_name, phone) values ('[email protected]', 'Rahul', NULL, 5515874956); | ||
INSERT INTO ecommerce.users_products (user_id, product_id) values (4, 2); | ||
INSERT INTO ecommerce.users_products (user_id, product_id) values (4, 5); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
The queries used in the lesson. | ||
|
||
SELECT * FROM products; | ||
SELECT id, name FROM products; | ||
SELECT id, name, category FROM products where id = 1; | ||
SELECT id, name, category FROM products where category = 'book'; | ||
SELECT id, first_name, last_name FROM users; | ||
SELECT id, first_name, last_name FROM users WHERE last_name = �unknown�; | ||
SELECT id, first_name, last_name, phone FROM users WHERE last_name = 'unknown' AND phone = 991517499; | ||
|
||
|
||
DISTINCT | ||
|
||
Run the following query: | ||
|
||
SELECT user_id FROM users_products; | ||
|
||
The above query returns all the users who have purchased products including many repeated users who have purchased more than one product. | ||
|
||
To get only the distinct users we use DISTINCT keyword as: | ||
|
||
SELECT DISTINCT user_id FROM users_products; | ||
|
||
|
||
ORDER BY | ||
|
||
ORDER BY is used to get the results in a sorted manner. Run the following query: | ||
|
||
SELECT id, name FROM products ORDER BY name ASC; | ||
|
||
ORDER BY is followed by the column name depending on which the records will be sorted. ASC is the keyword used to sort the records in the ascending order. DESC is the keyword used to sort the records in the descending order. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
The queries used in the lesson. | ||
|
||
UPDATE users SET first_name = 'Arjun' WHERE users.id = 3; | ||
UPDATE users SET first_name = 'Arjun' WHERE users.id = 3; | ||
UPDATE users SET first_name = 'Arjun' WHERE users.id = 3; | ||
UPDATE users SET first_name = 'Arjun'; | ||
UPDATE users SET first_name = 'Arjun' WHERE users.id = 3; | ||
UPDATE users SET first_name = 'Vinayak' WHERE users.email = '[email protected]'; | ||
UPDATE users SET last_name = 'unknown' WHERE last_name IS NULL; | ||
UPDATE users SET first_name = 'Vinayak' WHERE users.id = 1 OR users.id = 2; | ||
DELETE FROM users WHERE id = 1; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
The queries used in the lesson. | ||
|
||
SELECT u.first_name, u.phone, u.email FROM users_products up INNER JOIN users u ON u.id = up.user_id; | ||
SELECT u.first_name, u.phone, u.email, p.name FROM users_products up | ||
INNER JOIN users u ON u.id = up.user_id | ||
INNER JOIN products p ON p.id = up.product_id; | ||
SELECT u.first_name, u.phone, u.email, p.name FROM users_products up | ||
INNER JOIN users u ON u.id = up.user_id | ||
INNER JOIN products p ON p.id = up.product_id | ||
WHERE p.name = �deception_point�; | ||
SELECT u.first_name, u.phone, u.email, p.name FROM users_products up INNER JOIN users u | ||
ON u.id = up.user_id INNER JOIN products p ON p.id = up.product_id | ||
WHERE p.name = �deception_point� OR p.name = �animal_farm�; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The queries used in the lesson. | ||
|
||
SELECT * FROM users u LEFT JOIN users_products up ON u.id = up.user_id; | ||
SELECT * FROM users_products up LEFT JOIN users u ON up.user_id = u.id; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The queries used in the lesson. | ||
|
||
SELECT * FROM users_products INNER JOIN users ON users_products.user_id = users.id; | ||
SELECT * FROM users_products up INNER JOIN users u ON u.id = up.user_id; | ||
|