└── SQLNotes ├── AdvancedSQLSession1-CTE.txt ├── Session1-GettingStartedWithSQL.txt ├── Session10-LogicalOperators.txt ├── Session11-JoinsVeryImportantForInterviews.txt ├── Session12-WHEREandHAVINGClause.txt ├── Session13-OVERandPartitionByClause.txt ├── Session14-ROWNUMBERFunction.txt ├── Session15-RankandDenseRank.txt ├── Session2-CRUDoperations.txt ├── Session3-PrimaryKeyVsUniqueKey.txt ├── Session4-DDLVsDML.txt ├── Session5-ForeignKeyConstraint.txt ├── Session6-DistinctOderbyLimitKeyword.txt ├── Session7-OrderOfExecutionSQL.txt ├── Session8-AggregateFunctions.txt ├── Session9-FewMoreDatatypes.txt └── readme /SQLNotes/AdvancedSQLSession1-CTE.txt: -------------------------------------------------------------------------------- 1 | 2 | Common table expression (CTE) 3 | ============================== 4 | 5 | this is to simplify the complex queries. 6 | 7 | it improves the readability of a query 8 | 9 | Orders table 10 | ============= 11 | 12 | order_id, order_data, order_customer_id, order_status 13 | 14 | total orders each customer has placed 15 | 16 | I want to find out premium customers (who places more orders) 17 | 18 | 19 | average number of numbers placed by each customer 20 | 21 | Solution 1 (subquery) 22 | ====================== 23 | 24 | select avg(total_orders_per_customer) as avg_orders_per_customer from ( 25 | select order_customer_id, count(*) as total_orders_per_customer from orders group by order_customer_id) x 26 | 27 | 28 | Solution 2 (CTE/WITH CLAUSE) 29 | ============================ 30 | 31 | WITH total_orders (order_customer_id,total_orders_per_customer) as 32 | (select order_customer_id, count(*) as total_orders_per_customer from orders group by order_customer_id) 33 | 34 | select avg(total_orders_per_customer) as avg_orders_per_customer from total_orders 35 | 36 | subquery 37 | ========== 38 | query3(query2(query1)) 39 | 40 | CTE 41 | ==== 42 | query1 43 | 44 | query2 45 | 46 | query3 47 | 48 | 49 | I want to find premium customers who places more orders than the average number of orders 50 | =================== 51 | 52 | select * from (select order_customer_id, count(*) as total_orders_per_customer from orders group by order_customer_id) total_orders join(select avg(total_orders_per_customer) as avg_orders_per_customer from ( 53 | select order_customer_id, count(*) as total_orders_per_customer from orders group by order_customer_id) x) average_orders on total_orders.total_orders_per_customer > average_orders.avg_orders_per_customer 54 | 55 | 56 | 3 step process 57 | =============== 58 | 1. calculate total orders per customer 59 | 2. calculate average number of number orders for the customers 60 | 3. get to know the customers who are premium 61 | 62 | WITH total_orders(order_customer_id, total_orders_per_customer) as 63 | (select order_customer_id, count(*) as total_orders_per_customer from orders group by order_customer_id), 64 | average_orders(avg_orders_per_customer) as 65 | (select avg(total_orders_per_customer) as avg_orders_per_customer from total_orders) 66 | select * from orders; 67 | select * from total_orders join average_orders on total_orders.total_orders_per_customer > average_orders.avg_orders_per_customer; 68 | 69 | 70 | CTE defines a temporary resultset that you can refer in select, insert, update, delete statements immedicate follows the CTE 71 | 72 | 73 | 74 | WITH total_orders(order_customer_id, order_customer_id1, total_orders_per_customer) as 75 | (select order_customer_id, order_customer_id, count(*) as total_orders_per_customer from orders group by order_customer_id), 76 | average_orders(avg_orders_per_customer) as 77 | (select avg(total_orders_per_customer) as avg_orders_per_customer from total_orders) 78 | select * from total_orders join average_orders on total_orders.total_orders_per_customer > average_orders.avg_orders_per_customer; 79 | 80 | 81 | ==================== 82 | 83 | 84 | CTE can improve the performance by not always 85 | 86 | 87 | 88 | PostgreSQL 89 | =========== 90 | 91 | before version 12 92 | 93 | select * from orders where order_id = 10000; 94 | 95 | with orders_cte as 96 | ( 97 | select * from orders 98 | ) 99 | select * from orders_cte where order_id = 10000; 100 | 101 | on CTE the indexes are not performant 102 | 103 | before version 12 in postgreSQL 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /SQLNotes/Session1-GettingStartedWithSQL.txt: -------------------------------------------------------------------------------- 1 | SQL Session 1: 2 | 3 | ============================================== 4 | 5 | What is a database ? 6 | 7 | A collection of data and holds this data in form of tables. 8 | 9 | What is table ? 10 | 11 | A entity Holds data in form of rows and columns. 12 | 13 | it is similar to excel spreadsheet. 14 | 15 | The database provide us the capability to access and manipulate the data. 16 | 17 | 18 | 2 types of databases 19 | ================================================ 20 | 21 | 1) Relational Database: 22 | 23 | Rows and columns and also the tables have relation between them. 24 | 25 | Ex: Mysql, SQL Server, Postgres, SQL Lite, Maria DB and others 26 | 27 | 2) NOSQL database: 28 | 29 | Key value, Document, Graph. 30 | 31 | Ex: Hbase, MongoDB, Casandra and others. 32 | 33 | 34 | 35 | SQL : Structured Query Language and is used to query relational databases. 36 | 37 | Installation: 38 | 39 | goormIDE: mysql-ctl cli; 40 | 41 | To list databases: 42 | ================================ 43 | show databases; 44 | 45 | Create database: 46 | ================================= 47 | 48 | CREATE DATABASE trendytech; 49 | 50 | Drop database: 51 | 52 | ================================= 53 | 54 | drop database trendytech; 55 | 56 | 57 | How do I know which database I am in currently: 58 | 59 | ================================================ 60 | 61 | SELECT database(); 62 | 63 | Creating Table: 64 | 65 | ======================================= 66 | 67 | CREATE TABLE employee 68 | ( 69 | name VARCHAR(50), 70 | age INT, 71 | salary INT 72 | ) ; 73 | 74 | 75 | To List tables: 76 | 77 | =============================== 78 | 79 | show tables; 80 | 81 | Structure of Table: 82 | 83 | 84 | ===================================== 85 | 86 | describe table name; 87 | 88 | desc tablename; 89 | 90 | Drop Table: 91 | 92 | ====================================== 93 | 94 | drop table table name; 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /SQLNotes/Session10-LogicalOperators.txt: -------------------------------------------------------------------------------- 1 | Session 10: 2 | ======================== 3 | 4 | select * from students where location = 'bangalore'; 5 | 6 | To Get people who are not from bangalore: 7 | 8 | 9 | select * from students where location != 'bangalore'; 10 | 11 | 12 | seelct * from courses; 13 | 14 | get all the courses which has word data. 15 | 16 | select * from courses where course_name like "%data%"; 17 | 18 | 19 | select * from courses where course_name like "%data%"; 20 | +-----------+-------------+---------------------------+------------+ 21 | | course_id | course_name | course_duration_in_months | course_fee | 22 | +-----------+-------------+---------------------------+------------+ 23 | | 1 | bigdata | 6 | 40000 | 24 | | 3 | datascience | 6 | 50000 | 25 | | 5 | database | 4 | 3000 | 26 | +-----------+-------------+---------------------------+------------+ 27 | 28 | 29 | get all courses which do not have the word keyword: 30 | 31 | 32 | select * from courses where course_name not like "%data%"; 33 | 34 | 35 | select * from courses where course_name not like "%data%"; 36 | +-----------+----------------+---------------------------+------------+ 37 | | course_id | course_name | course_duration_in_months | course_fee | 38 | +-----------+----------------+---------------------------+------------+ 39 | | 2 | webdevelopment | 3 | 20000 | 40 | | 4 | devops | 1 | 10000 | 41 | +-----------+----------------+---------------------------+------------+ 42 | 43 | 44 | All students from bangalore who joined through linked in and having less than 8 years of exp; 45 | 46 | select * from students where years_of_exp < 8 and and source_of_joining = 'linkedin' and location='bangalore'; 47 | 48 | 49 | I want all people who do not fall between 8 to 12 years of exp; 50 | 51 | select * from students where years_of_exp<8 or years_of_exp>12; 52 | 53 | select * from students where years_of_exp not between 8 and 12; 54 | 55 | 56 | List of students for flipkart walmart or microsoft 57 | 58 | select * from students where student_company in ('flipkart','walmart','microsoft'); 59 | 60 | select * from students where student_company not in ('flipkart','walmart','microsoft'); 61 | 62 | 63 | If course more than 4 months we categorize as Master's program else diploma program. 64 | 65 | 66 | 67 | 68 | 69 | select course_id,course_name,course_fee, 70 | CASE 71 | WHEN course_duration_in_months > 4 THEN 'masters' 72 | ELSE 'diploma' 73 | END as course_type from courses; 74 | 75 | 76 | 77 | select course_id,course_name,course_fee, 78 | -> CASE 79 | -> WHEN course_duration_in_months > 4 THEN 'masters' 80 | -> ELSE 'diploma' 81 | -> END as course_type from courses; 82 | +-----------+----------------+------------+-------------+ 83 | | course_id | course_name | course_fee | course_type | 84 | +-----------+----------------+------------+-------------+ 85 | | 1 | bigdata | 40000 | masters | 86 | | 2 | webdevelopment | 20000 | diploma | 87 | | 3 | datascience | 50000 | masters | 88 | | 4 | devops | 10000 | diploma | 89 | | 5 | database | 3000 | diploma | 90 | +-----------+----------------+------------+-------------+ 91 | 92 | 93 | People working for walmart , flipkar and microsoft we want to say product based else sevrice based 94 | 95 | 96 | select student_id,student_fname,student_lname,student_company, 97 | CASE 98 | WHEN student_company in ('flipkart','walmart','microsoft') THEN 'product based' 99 | ELSE 'service based' 100 | END as company_type from students; 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /SQLNotes/Session11-JoinsVeryImportantForInterviews.txt: -------------------------------------------------------------------------------- 1 | ----- 2 | JOINS 3 | ----- 4 | 5 | -- Find the course in which students are enrolled 6 | SELECT student_fname, student_lname, selected_course, course_name FROM students s 7 | JOIN courses c ON s.selected_course = c.course_id; 8 | 9 | ** By default, it is a INNER JOIN 10 | 11 | ** INNER JOIN : only the matching records will be considered and non-matching records will be discarded 12 | 13 | --------------- 14 | LEFT OUTER JOIN 15 | --------------- 16 | 17 | *** All the matching records from left and right table are considered + 18 | All the non matching records in the left table which does not have the match in the right padded with NULL 19 | 20 | ** For LEFT OUTER JOIN to happen, we must have some extra records in the left table(T1) 21 | 22 | ** T1(Left Table) X T2(Right Table) 23 | 24 | -- Delete the course having course_id = 2 25 | 26 | DELETE FROM courses WHERE course_id = 2; 27 | 28 | Error: ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`trendytech`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`selected_course`) REFER 29 | ENCES `courses` (`course_id`)) 30 | 31 | * The above DELETE query won't work because it is attached with the students table also in the form of FOREIGN KEY 32 | 33 | * If we try to delete the course having course_id = 2, then the student who has enrolled for course_id = 2 will become obsolete 34 | 35 | -- Now, we will create a new students table and new couses table with same data but FOREIGN key constraint won't be there 36 | 37 | CREATE TABLE students_latest AS SELECT * FROM students; 38 | CREATE TABLE courses_latest AS SELECT * FROM courses; 39 | 40 | -- Now, we can delete the records from courses table 41 | 42 | -- Delete the course having course_id = 2 43 | 44 | DELETE FROM courses_latest WHERE course_id = 2; 45 | 46 | -- What will happen if we perform INNER JOIN on the above created table 47 | 48 | -- Find the course in which students are enrolled 49 | SELECT student_fname, student_lname, selected_course, course_name FROM students_latest s 50 | JOIN courses_latest c ON s.selected_course = c.course_id; 51 | 52 | -- Now, lets perform a LEFT OUTER JOIN 53 | 54 | ** How to perform a LEFT OUTER JOIN? 55 | ==> We just have to give the keyword LEFT before JOIN 56 | 57 | -- Find the course in which students are enrolled 58 | SELECT student_fname, student_lname, selected_course, course_name FROM students_latest s 59 | LEFT JOIN courses_latest c ON s.selected_course = c.course_id; 60 | 61 | ---------------- 62 | RIGHT OUTER JOIN 63 | ---------------- 64 | 65 | *** All the matching records from left and right table are considered + 66 | All the non matching records in the right table which does not have the match in the left padded with NULL 67 | 68 | ** For RIGHT OUTER JOIN to happen, we must have some extra records in the right table(T2) 69 | 70 | ** T1(Left Table) X T2(Right Table) 71 | ** How to perform a RIGHT OUTER JOIN? 72 | ==> We just have to give the keyword RIGHT before JOIN 73 | 74 | -- Find the course in which students are enrolled 75 | SELECT student_fname, student_lname, selected_course, course_name FROM students_latest s 76 | RIGHT JOIN courses_latest c ON s.selected_course = c.course_id; 77 | 78 | --------------- 79 | FULL OUTER JOIN 80 | --------------- 81 | 82 | * Combination of LEFT OUTER JOIN and RIGHT OUTER JOIN. 83 | 84 | * FULL OUTER JOIN = LEFT OUTER JOIN UNION RIGHT OUTER JOIN 85 | 86 | * All the matching records + Non-matching records from left + Non-matching records from right 87 | 88 | * In SQL, we do not have keyword FULL for FULL OUTER JOIN, so we will do UNION of LEFT OUTER JOIN and RIGHT OUTER JOIN 89 | 90 | SELECT student_fname, student_lname, selected_course, course_name FROM students_latest s 91 | LEFT JOIN courses_latest c ON s.selected_course = c.course_id 92 | UNION 93 | SELECT student_fname, student_lname, selected_course, course_name FROM students_latest s 94 | RIGHT JOIN courses_latest c ON s.selected_course = c.course_id; 95 | 96 | ---------- 97 | CROSS JOIN 98 | ---------- 99 | * CROSS JOIN is costly as each record of first table is getting combined with each record of second table. 100 | 101 | If Left Table (T1) has 4 records 102 | If Right Table (T2) has 5 records 103 | 104 | Then, CROSS JOIN = 4 * 5 = 20 records, 1 to 1 mapping with each records 105 | 106 | SELECT COUNT(*) FROM students, courses; 107 | -- 35 records 108 | 109 | SELECT COUNT(*) FROM students JOIN courses; 110 | -- 35 records 111 | 112 | SELECT COUNT(*) FROM students; 113 | -- 7 records 114 | 115 | SELECT COUNT(*) FROM courses; 116 | -- 5 records -------------------------------------------------------------------------------- /SQLNotes/Session12-WHEREandHAVINGClause.txt: -------------------------------------------------------------------------------- 1 | ----------------------------- 2 | WHERE vs HAVING clause in SQL 3 | ----------------------------- 4 | 5 | -- Find the source_of_joining through which more than 1 student has enrolled 6 | 7 | SELECT source_of_joining, COUNT(*) AS total from students GROUP BY source_of_joining WHERE total > 1; 8 | 9 | Error: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE total > 1' at 10 | line 1 11 | 12 | ** WHERE clause is used to filter the individual records before GROUP BY or aggregation. 13 | 14 | SELECT source_of_joining, COUNT(*) AS total from students GROUP BY source_of_joining HAVING total > 1; 15 | 16 | ** HAVING clause is used to filter the records after GROUP BY or aggregation. 17 | 18 | -- Find the total number of students who have enrolled through linkedin as source_of_joining 19 | 20 | -- OPTIMISED QUERY (Filtering is happening before aggregation) 21 | SELECT source_of_joining, COUNT(*) AS total FROM students WHERE source_of_joining = 'linkedin' GROUP BY source_of_joining; 22 | 23 | or 24 | 25 | -- NON-OPTIMISEZ QUERY (Filtering is happening after aggregation) 26 | SELECT source_of_joining, COUNT(*) AS total FROM students GROUP BY source_of_joining HAVING source_of_joining = 'linkedin'; 27 | 28 | ------------------------------------------------------ 29 | Can we use WHERE and HAVING clause in the same query ? 30 | ------------------------------------------------------ 31 | 32 | -- Find the locations from which more than 1 student has joined & the students years_of_exp is more than 5 years 33 | 34 | SELECT location, COUNT(*) AS total from students WHERE years_of_exp > 5 GROUP BY location HAVING total > 1; 35 | 36 | ======================================================================== 37 | ** WHERE is used before GROUP BY and do filtering on individual records. 38 | 39 | ** HAVING is used after GROUP BY and do filtering on aggregated records. 40 | 41 | ** We can use WHERE and HAVING in the same query also. 42 | 43 | ** WHERE is more performant than HAVING. -------------------------------------------------------------------------------- /SQLNotes/Session13-OVERandPartitionByClause.txt: -------------------------------------------------------------------------------- 1 | ---------------------------- 2 | OVER and PARTITION BY clause 3 | ---------------------------- 4 | 5 | -- From here we won't use goorm IDE, because that was using older versions of MySQL instead we will use https://onecompiler.com/mysql 6 | 7 | CREATE TABLE employee( 8 | firstname varchar(20), 9 | lastname varchar(20), 10 | age int, 11 | salary int, 12 | location varchar(20) 13 | ); 14 | 15 | INSERT INTO employee VALUES 16 | ('Sachin', 'Sharma', 28, 10000, 'Bangalore'), 17 | ('Rohit', 'Sharma', 30, 20000, 'Hyderabad'), 18 | ('Virat', 'Kohli', 29, 30000, 'Chennai'), 19 | ('Akshay', 'Kumar', 32, 40000, 'Banagalore'), 20 | ('Sunil', 'Shetty', 34, 50000, 'Hyderabad'), 21 | ('Mark', 'John', 34, 50000, 'Bangalore'); 22 | 23 | -- How many people are from each location and average salary at each location 24 | 25 | -- Bangalore, 4, 19999 (Example) 26 | 27 | SELECT location, COUNT(location) AS total, AVG(salary) AS average from employee GROUP BY location; 28 | 29 | -- Fetch the firstname, latname and how many people are from each location and average salary at each location 30 | 31 | SELECT firstname, lastname, location, COUNT(location) AS total, AVG(salary) AS average from employee GROUP BY location; 32 | 33 | ERROR 1055 (42000) at line 19: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_3ye8guejd.employee.firstname' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 34 | 35 | -- We can try achieving it using a JOIN 36 | 37 | SELECT firstname, lastname, employee.location, total_count, avg_salary FROM employee JOIN 38 | (SELECT location, COUNT(location) AS total_count, AVG(salary) AS avg_salary from employee GROUP BY location) temptable ON 39 | employee.location = temptable.location; 40 | 41 | -- Is there any other way to perform above query? 42 | 43 | ==> We can use OVER PARTITION BY to achieve this easily 44 | 45 | ** PARTITION BY is useful while performing operations on Non-Aggregate columns 46 | 47 | SELECT firstname, lastname, location, 48 | COUNT(location) OVER (PARTITION BY location) AS total, 49 | AVG(salary) OVER (PARTITION BY location) AS average 50 | FROM employee; 51 | -------------------------------------------------------------------------------- /SQLNotes/Session14-ROWNUMBERFunction.txt: -------------------------------------------------------------------------------- 1 | ------------------- 2 | ROW NUMBER FUNCTION 3 | ------------------- 4 | 5 | CREATE TABLE employee( 6 | firstname varchar(20), 7 | lastname varchar(20), 8 | age int, 9 | salary int, 10 | location varchar(20) 11 | ); 12 | 13 | INSERT INTO employee VALUES 14 | ('Sachin', 'Sharma', 28, 10000, 'Bangalore'), 15 | ('Shane', 'Warne', 30, 20000, 'Bangalore'), 16 | ('Rohit', 'Sharma', 32, 30000, 'Hyderabad'), 17 | ('Shikhar', 'Dhawan', 32, 25000, 'Hyderabad'), 18 | ('Rahul', 'Dravid', 31, 20000, 'Banagalore'), 19 | ('Saurabh', 'Ganguly', 32, 15000, 'Pune'), 20 | ('Kapil', 'Dev', 34, 10000, 'Pune'); 21 | 22 | ===================================== 23 | SELECT firstname, lastname, salary, 24 | row_number() as rownum OVER (ORDER BY salary DESC) FROM employee; 25 | 26 | ** ROW_NUMBER() won't work without ORDER BY 27 | 28 | -- Find the record having 5th highest salary 29 | 30 | SELECT * FROM (SELECT firstname, lastname, salary, 31 | row_number() OVER (ORDER BY salary DESC) AS rownum FROM employee) temptable WHERE rownum = 5; 32 | 33 | -- Assign row number for partitions based on each location 34 | 35 | SELECT firstname, lastname, location, salary, row_number() OVER (PARTITION BY location ORDER BY salary DESC) FROM employee; 36 | 37 | -- Find the candidate having highest salary from each location 38 | 39 | SELECT * FROM (SELECT firstname, lastname, location, salary, 40 | row_number() OVER (PARTITION BY location ORDER BY salary DESC) AS rownum 41 | FROM employee) temptable 42 | WHERE rownum = 1; 43 | 44 | =============================================================================== 45 | ** When we use ROW_NUMBER, we must use ORDER BY clause otherwise it won't work 46 | ** When we use ROW_NUMBER, we may/may not use the PARTITION BY 47 | ** The ROW_NUMBER starts from 1 for each partition 48 | ** ROW_NUMBER will assign the number uniquely even though there are duplicate data in the records -------------------------------------------------------------------------------- /SQLNotes/Session15-RankandDenseRank.txt: -------------------------------------------------------------------------------- 1 | ----------------- 2 | RANK & DENSE RANK 3 | ----------------- 4 | 5 | CREATE TABLE employee( 6 | firstname varchar(20), 7 | lastname varchar(20), 8 | age int, 9 | salary int, 10 | location varchar(20) 11 | ); 12 | 13 | INSERT INTO employee VALUES 14 | ('Sachin', 'Sharma', 28, 10000, 'Bangalore'), 15 | ('Shane', 'Warne', 30, 20000, 'Bangalore'), 16 | ('Rohit', 'Sharma', 32, 30000, 'Hyderabad'), 17 | ('Shikhar', 'Dhawan', 32, 25000, 'Hyderabad'), 18 | ('Rahul', 'Dravid', 31, 21000, 'Banagalore'), 19 | ('Saurabh', 'Ganguly', 32, 15000, 'Pune'), 20 | ('Kapil', 'Dev', 34, 10000, 'Pune'); 21 | 22 | ---------------------------------------- 23 | ** Ideally, we should use ROW_NUMBER when there are no duplicate records 24 | ** Like in ROW_NUMBER, ORDER BY is mandatory for RANK & DENSE_RANK 25 | -- Like in the above Example, Sachin and Kapil are having same salary so which one will get which row number we don't know. 26 | -- ROW_NUMBER do not handles duplicate, so we have RANK & DENSE RANK 27 | 28 | -- With ROW_NUMBER() 29 | SELECT firstname, lastname, salary, 30 | row_number() OVER (ORDER BY salary DESC) FROM employee; 31 | 32 | -- With RANK() 33 | SELECT firstname, lastname, salary, 34 | RANK() OVER (ORDER BY salary DESC) FROM employee; 35 | 36 | -- With DENSE_RANK() 37 | SELECT firstname, lastname, salary, 38 | DENSE_RANK() OVER (ORDER BY salary DESC) FROM employee; 39 | 40 | ** RANK() will skip assigning row numbers to records when there are duplicate data in the records. 41 | ** RANK() will assign the same rank for duplicate data in the records. 42 | 43 | ** DENSE_RANK() will assign the row numbers in sequence even when there are duplicate data in the records. 44 | 45 | ** When there are no duplicate data in the records, ROW_NUMBER(), RANK() and DENSE_RANK() all of them will give the same result. 46 | 47 | ==================================================================== 48 | ** When there are no duplicates in the records then use ROW_NUMBER 49 | 50 | When should we use the DENSE_RANK() ? 51 | 52 | -- Consider the usecase when we have to find the top 3 positions in some competition, then as per DENSE_RANK(), we will have the 53 | below ranks 54 | 55 | Candidate 1 score = 100 ==> 1 56 | Candidate 2 score = 100 ==> 1 57 | Candidate 3 score = 98 ==> 2 58 | Candidate 4 score = 97 ==> 3 59 | Candidate 5 score = 97 ==> 3 60 | Candidate 6 score = 96 ==> 4 61 | 62 | -- Find the person having rank = 4 63 | 64 | SELECT * FROM (SELECT firstname, lastname, salary, 65 | DENSE_RANK() OVER (ORDER BY salary DESC) AS rownum from employee) temptable WHERE rownum = 4; -------------------------------------------------------------------------------- /SQLNotes/Session2-CRUDoperations.txt: -------------------------------------------------------------------------------- 1 | Learn the SQL right way Session 2: 2 | ============================================== 3 | 4 | CRUD Operations 5 | 6 | create --- Insert statements 7 | read --- select statements 8 | update --- update statements 9 | delete ---- delete statements 10 | 11 | Creation of Table and Insert statements: 12 | 13 | ========================================= 14 | 15 | employee table 16 | 17 | prefer to hire from bangalore location 18 | 19 | 20 | 21 | CREATE TABLE employee ( 22 | firstname varchar(20), 23 | middlename varchar(20), 24 | lastname varchar(20), 25 | age INT, 26 | salary INT, 27 | location varchar(20) 28 | ) 29 | 30 | describe employee; 31 | 32 | we are doing something with definition is called DDL 33 | 34 | show tables; 35 | 36 | select * from employee; 37 | 38 | inserting data into Table: 39 | ============================= 40 | 41 | INSERT INTO employee(firstname, middlename, lastname, age, salary, location) VALUES ('satya','nara','yana',36,60000,'bangalore'); 42 | 43 | 44 | INSERT INTO employee(firstname, middlename, lastname, age, salary, location) VALUES ('dhruvan','kumar','bhima',17,80000,'bangalore'); 45 | 46 | How to insert data into only few columns: 47 | =============================================== 48 | 49 | 50 | INSERT INTO employee(firstname, lastname, age, salary, location) VALUES ('Rajesh','sharma',28,10000,'bangalore'); 51 | 52 | INSERT INTO employee(firstname, lastname, age, salary, location) VALUES ("Ritesh's",'sharma',28,10000,'bangalore'); 53 | 54 | INSERT INTO employee(firstname, lastname, age, salary, location) VALUES ('Ritesh\'s','sharma',28,10000,'bangalore'); 55 | 56 | 57 | How to insert multiple values in one Go: 58 | 59 | ============================================================================= 60 | 61 | INSERT INTO employee(firstname, middlename, lastname, age, salary, location) VALUES ('Kapil','Kumar','Sinha',28,10000,'bangalore'),('Satish','Kumar','Sinha',30,20000,'bangalore'); 62 | 63 | Datatype Mismatch: 64 | ====================================== 65 | 66 | INSERT INTO employee(firstname, middlename, lastname, age, salary, location) VALUES ('Kapildddddddddddddddddddddddjjjjjjjj','Kumar','Sinha',28,10000,'bangalore'); 67 | 68 | ERROR 1406 (22001): Data too long for column 'firstname' at row 1 69 | 70 | NULL 71 | ===================================== 72 | 73 | 74 | INSERT INTO employee(firstname, lastname, age, salary) VALUES ('Kumar','Sinha',29,10000); 75 | 76 | Creating Table without NULL: 77 | 78 | ==================================== 79 | 80 | CREATE TABLE employee ( 81 | firstname varchar(20) NOT NULL, 82 | middlename varchar(20), 83 | lastname varchar(20) NOT NULL, 84 | age INT NOT NULL, 85 | salary INT NOT NULL, 86 | location varchar(20) NOT NULL 87 | ); 88 | 89 | INSERT INTO employee(middlename, lastname, age, salary, location) VALUES ('Kumar','Sinha',29,10000,'ba 90 | ngalore'); 91 | ERROR 1364 (HY000): Field 'firstname' doesn't have a default value. 92 | 93 | Default Values: 94 | 95 | ================================== 96 | 97 | CREATE TABLE employee ( 98 | firstname varchar(20) NOT NULL, 99 | middlename varchar(20), 100 | lastname varchar(20) NOT NULL, 101 | age INT NOT NULL, 102 | salary INT NOT NULL, 103 | location varchar(20) DEFAULT 'BANGLORE' 104 | ); 105 | 106 | 107 | INSERT INTO employee(firstname, lastname, age, salary, location) VALUES ('satish','kumar',29,10000,'hyderabad'); 108 | 109 | 110 | INSERT INTO employee(firstname, lastname, age, salary, location) VALUES ('rajesh','kumar',29,10000,null); 111 | 112 | 113 | 114 | Combination of NOT NOT and DEFAULT 115 | 116 | ========================================= 117 | CREATE TABLE employee ( 118 | firstname varchar(20) NOT NULL, 119 | middlename varchar(20), 120 | lastname varchar(20) NOT NULL, 121 | age INT NOT NULL, 122 | salary INT NOT NULL, 123 | location varchar(20) NOT NULL DEFAULT 'BANGLORE' 124 | ); 125 | 126 | INSERT INTO employee(firstname, lastname, age, salary) VALUES ('rajesh','kumar',29,10000); 127 | l); 128 | ERROR 1048 (23000): Column 'location' cannot be null 129 | 130 | INSERT INTO employee(firstname, lastname, age, salary) VALUES ('rajesh','kumar',29,10000); 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /SQLNotes/Session3-PrimaryKeyVsUniqueKey.txt: -------------------------------------------------------------------------------- 1 | Session 3: Primary Key Vs Unique Key 2 | ========================================= 3 | 4 | 5 | 6 | 7 | Primaty Key, 8 | 9 | Auto Increment Keys 10 | 11 | Unique Key 12 | 13 | Primary key vs unique key 14 | 15 | INSERT INTO employee(firstname, lastname, age, salary) VALUES ('rajesh','kumar',29,10000); 16 | 17 | 18 | Primary Key 19 | ============================== 20 | It helps us to uniquely identify in the table. 21 | 22 | 23 | CREATE TABLE employee ( 24 | id INT, 25 | firstname varchar(20) NOT NULL, 26 | middlename varchar(20), 27 | lastname varchar(20) NOT NULL, 28 | age INT NOT NULL, 29 | salary INT NOT NULL, 30 | location varchar(20) NOT NULL DEFAULT 'BANGLORE' 31 | ); 32 | 33 | INSERT INTO employee(id,firstname, lastname, age, salary) VALUES (1,'rajesh','kumar',29,10000),(2,'satish','kumar',28,30000); 34 | 35 | Creating table with Primary Key: 36 | =================================== 37 | 38 | CREATE TABLE employee ( 39 | id INT PRIMARY KEY, 40 | firstname varchar(20) NOT NULL, 41 | middlename varchar(20), 42 | lastname varchar(20) NOT NULL, 43 | age INT NOT NULL, 44 | salary INT NOT NULL, 45 | location varchar(20) NOT NULL DEFAULT 'BANGLORE' 46 | ); 47 | 48 | INSERT INTO employee(id,firstname, lastname, age, salary) VALUES (3,'maneesh','kumar',30,10000) 49 | 50 | For a primary key null values are not allowed and repeated values also not allowed. 51 | 52 | INSERT INTO employee(id,firstname, lastname, age, salary) VALUES (NULL,'rejina','kumar',29,10000); 53 | ERROR 1048 (23000): Column 'id' cannot be null 54 | 55 | INSERT INTO employee(id,firstname, lastname, age, salary) VALUES (1,'rejina','kumar',29,10000); 56 | 57 | 58 | INSERT INTO employee(id,firstname, lastname, age, salary) VALUES (1,'rejina','kumar',29,10000); 59 | ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' 60 | 61 | other way to define the primary key: 62 | 63 | ===================================== 64 | CREATE TABLE employee ( 65 | id INT, 66 | firstname varchar(20) NOT NULL, 67 | middlename varchar(20), 68 | lastname varchar(20) NOT NULL, 69 | age INT NOT NULL, 70 | salary INT NOT NULL, 71 | location varchar(20) NOT NULL DEFAULT 'BANGLORE', 72 | PRIMARY KEY(id) 73 | ); 74 | 75 | 76 | 77 | Composite Primary Key: 78 | 79 | CREATE TABLE employee ( 80 | id INT, 81 | firstname varchar(20) NOT NULL, 82 | middlename varchar(20), 83 | lastname varchar(20) NOT NULL, 84 | age INT NOT NULL, 85 | salary INT NOT NULL, 86 | location varchar(20) NOT NULL DEFAULT 'BANGLORE', 87 | PRIMARY KEY(id,firstname) 88 | ); 89 | 90 | 91 | creating Table with Autoincrement: 92 | 93 | ====================================== 94 | 95 | 96 | CREATE TABLE employee ( 97 | id INT AUTO_INCREMENT, 98 | firstname varchar(20) NOT NULL, 99 | middlename varchar(20), 100 | lastname varchar(20) NOT NULL, 101 | age INT NOT NULL, 102 | salary INT NOT NULL, 103 | location varchar(20) NOT NULL DEFAULT 'BANGLORE', 104 | PRIMARY KEY(id) 105 | ); 106 | 107 | 108 | 109 | 110 | INSERT INTO employee(firstname, lastname, age, salary) VALUES ('rejina','kumar',29,10000),('satish','ku 111 | mar',29,10000),('brijexh','kumar',29,10000); 112 | Query OK, 3 rows affected (0.02 sec) 113 | Records: 3 Duplicates: 0 Warnings: 0 114 | 115 | mysql> select * from employee; 116 | +----+-----------+------------+----------+-----+--------+----------+ 117 | | id | firstname | middlename | lastname | age | salary | location | 118 | +----+-----------+------------+----------+-----+--------+----------+ 119 | | 1 | rejina | NULL | kumar | 29 | 10000 | BANGLORE | 120 | | 2 | satish | NULL | kumar | 29 | 10000 | BANGLORE | 121 | | 3 | brijexh | NULL | kumar | 29 | 10000 | BANGLORE | 122 | +----+-----------+------------+----------+-----+--------+----------+ 123 | 124 | 125 | Unique Key: 126 | 127 | =========================================== 128 | 129 | 130 | 131 | You can have only one primary key and primary key can not hold any null value. 132 | 133 | We shopuld use primary key when we have to identify each record. 134 | 135 | 136 | Unique key can hold NULL values 137 | 138 | In MYSQL Unique key can hold any number of NULL values, 139 | 140 | in some of other famous DB's only it allow only NULL. 141 | 142 | The purpose of unique key is to make sure the values do not duplicate. 143 | 144 | You can have only one primary key but multiple unique keys in table. 145 | 146 | 147 | 148 | CREATE TABLE employee ( 149 | firstname varchar(20) NOT NULL, 150 | lastname varchar(20) NOT NULL, 151 | age INT NOT NULL, 152 | PRIMARY KEY(firstname,lastname) 153 | ); 154 | 155 | 156 | insert into employee values ('kapil', 'sharma', 28); 157 | 158 | insert into employee values ('kapil', 'sinha', 28); 159 | 160 | 161 | CREATE TABLE employee ( 162 | id INT UNIQUE KEY, 163 | firstname varchar(20) , 164 | lastname varchar(20) , 165 | age INT 166 | ); 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | -------------------------------------------------------------------------------- /SQLNotes/Session4-DDLVsDML.txt: -------------------------------------------------------------------------------- 1 | SQL SESSION 4: 2 | ====================================== 3 | 4 | 5 | Primary Key 6 | 7 | Uniue Key 8 | 9 | Auto increment 10 | 11 | CRUD Operations: 12 | 13 | Create Insert 14 | 15 | Read Select 16 | 17 | Update Update 18 | 19 | Delete Delete 20 | 21 | 22 | CREATE TABLE employee ( 23 | id INT, 24 | firstname varchar(20) NOT NULL, 25 | middlename varchar(20), 26 | lastname varchar(20) NOT NULL, 27 | age INT NOT NULL, 28 | salary INT NOT NULL, 29 | location varchar(20) NOT NULL DEFAULT 'BANGLORE', 30 | PRIMARY KEY(id) 31 | ); 32 | 33 | 34 | Select 35 | =================== 36 | 37 | 1) selecting all columns: 38 | 39 | select * from employee; 40 | 41 | 2) Selecting specific columns. 42 | 43 | select firstname,lastname from employee; 44 | +-----------+----------+ 45 | | firstname | lastname | 46 | +-----------+----------+ 47 | | rajesh | kumar | 48 | | satish | kumar | 49 | | rajesh | kumar | 50 | +-----------+----------+ 51 | 52 | 3) How to see list whose age is greater than 25; 53 | 54 | Select by applying where clause. 55 | 56 | select * from employee where age > 25; 57 | +----+-----------+------------+----------+-----+--------+----------+ 58 | | id | firstname | middlename | lastname | age | salary | location | 59 | +----+-----------+------------+----------+-----+--------+----------+ 60 | | 1 | rajesh | NULL | kumar | 29 | 10000 | BANGLORE | 61 | | 2 | satish | NULL | kumar | 28 | 30000 | BANGLORE | 62 | | 3 | rajesh | NULL | kumar | 29 | 10000 | BANGLORE | 63 | +----+-----------+------------+----------+-----+--------+----------+ 64 | 65 | 4) Binary for case 66 | 67 | By default it is case insensitive. 68 | 69 | select * from employee where binary firstname ='Rajesh'; 70 | 71 | This stmt match exact case for case sensitive. 72 | 73 | Empty set (0.00 sec) 74 | 75 | mysql> select * from employee where firstname ='Rajesh'; 76 | +----+-----------+------------+----------+-----+--------+----------+ 77 | | id | firstname | middlename | lastname | age | salary | location | 78 | +----+-----------+------------+----------+-----+--------+----------+ 79 | | 1 | rajesh | NULL | kumar | 29 | 10000 | BANGLORE | 80 | 81 | 82 | 5) 83 | 84 | Alias: Use As Keyword. 85 | 86 | select firstname as name,lastname as surname from employee; 87 | +--------+---------+ 88 | | name | surname | 89 | +--------+---------+ 90 | | rajesh | kumar | 91 | | satish | kumar | 92 | | rajesh | kumar | 93 | +--------+---------+ 94 | 95 | Update: 96 | ================================ 97 | 98 | 99 | update employee set lastname = 'sharma' where firstname = 'maneesh'; 100 | Query OK, 1 row affected (0.01 sec) 101 | Rows matched: 1 Changed: 1 Warnings: 0 102 | 103 | mysql> select * from employee; 104 | +----+-----------+------------+----------+-----+--------+----------+ 105 | | id | firstname | middlename | lastname | age | salary | location | 106 | +----+-----------+------------+----------+-----+--------+----------+ 107 | | 1 | rajesh | NULL | kumar | 29 | 10000 | BANGLORE | 108 | | 2 | satish | NULL | kumar | 28 | 30000 | BANGLORE | 109 | | 3 | maneesh | NULL | sharma | 30 | 10000 | BANGLORE | 110 | +----+-----------+------------+----------+-----+--------+----------+ 111 | 112 | 113 | update employee set location = 'hyderabad' where firstname = 'satish'; 114 | Query OK, 0 rows affected (0.01 sec) 115 | Rows matched: 1 Changed: 0 Warnings: 0 116 | 117 | mysql> select * from employee; 118 | +----+-----------+------------+----------+-----+--------+-----------+ 119 | | id | firstname | middlename | lastname | age | salary | location | 120 | +----+-----------+------------+----------+-----+--------+-----------+ 121 | | 1 | rajesh | NULL | kumar | 29 | 10000 | BANGLORE | 122 | | 2 | satish | NULL | kumar | 28 | 30000 | hyderabad | 123 | | 3 | maneesh | NULL | sharma | 30 | 10000 | BANGLORE | 124 | +----+-----------+------------+----------+-----+--------+-----------+ 125 | 126 | 127 | update employee set salary=salary+5000; 128 | Query OK, 3 rows affected (0.01 sec) 129 | Rows matched: 3 Changed: 3 Warnings: 0 130 | 131 | mysql> select * from employee; 132 | +----+-----------+------------+----------+-----+--------+-----------+ 133 | | id | firstname | middlename | lastname | age | salary | location | 134 | +----+-----------+------------+----------+-----+--------+-----------+ 135 | | 1 | rajesh | NULL | kumar | 29 | 15000 | BANGLORE | 136 | | 2 | satish | NULL | kumar | 28 | 35000 | hyderabad | 137 | | 3 | maneesh | NULL | sharma | 30 | 15000 | BANGLORE | 138 | +----+-----------+------------+----------+-----+--------+-----------+ 139 | 140 | update employee set location = 'banglore' where firstname ='satish' and lastname = 'kumar'; 141 | Query OK, 1 row affected (0.01 sec) 142 | Rows matched: 1 Changed: 1 Warnings: 0 143 | 144 | mysql> select * from employee; 145 | +----+-----------+------------+----------+-----+--------+----------+ 146 | | id | firstname | middlename | lastname | age | salary | location | 147 | +----+-----------+------------+----------+-----+--------+----------+ 148 | | 1 | rajesh | NULL | kumar | 29 | 15000 | BANGLORE | 149 | | 2 | satish | NULL | kumar | 28 | 35000 | banglore | 150 | | 3 | maneesh | NULL | sharma | 30 | 15000 | BANGLORE | 151 | +----+-----------+------------+----------+-----+--------+----------+ 152 | 153 | 154 | DELETE: 155 | ========================================= 156 | 157 | DELETE from employee where id = 3; 158 | Query OK, 1 row affected (0.02 sec) 159 | 160 | mysql> select * from employee; 161 | +----+-----------+------------+----------+-----+--------+----------+ 162 | | id | firstname | middlename | lastname | age | salary | location | 163 | +----+-----------+------------+----------+-----+--------+----------+ 164 | | 1 | rajesh | NULL | kumar | 29 | 15000 | BANGLORE | 165 | | 2 | satish | NULL | kumar | 28 | 35000 | banglore | 166 | +----+-----------+------------+----------+-----+--------+----------+ 167 | 168 | delete from employee; 169 | Query OK, 2 rows affected (0.08 sec) 170 | 171 | mysql> select * from employee; 172 | Empty set (0.00 sec) 173 | 174 | 175 | ALTER: 176 | 177 | ================================ 178 | 179 | 180 | Alter is to alter the structure of the table. 181 | 182 | alter table employee add column jobtitle varchar(50); 183 | 184 | 185 | alter table employee add column jobtitle varchar(50); 186 | 187 | 188 | Query OK, 0 rows affected (0.19 sec) 189 | Records: 0 Duplicates: 0 Warnings: 0 190 | 191 | mysql> DESC employee; 192 | +------------+-------------+------+-----+----------+-------+ 193 | | Field | Type | Null | Key | Default | Extra | 194 | +------------+-------------+------+-----+----------+-------+ 195 | | id | int(11) | NO | PRI | NULL | | 196 | | firstname | varchar(20) | NO | | NULL | | 197 | | middlename | varchar(20) | YES | | NULL | | 198 | | lastname | varchar(20) | NO | | NULL | | 199 | | age | int(11) | NO | | NULL | | 200 | | salary | int(11) | NO | | NULL | | 201 | | location | varchar(20) | NO | | BANGLORE | | 202 | | jobtitle | varchar(50) | YES | | NULL | | 203 | +------------+-------------+------+-----+----------+-------+ 204 | 205 | alter table employee drop column jobtitle ; 206 | 207 | alter table employee drop column jobtitle ; 208 | Query OK, 0 rows affected (0.12 sec) 209 | Records: 0 Duplicates: 0 Warnings: 0 210 | 211 | mysql> DESC employee; 212 | +------------+-------------+------+-----+----------+-------+ 213 | | Field | Type | Null | Key | Default | Extra | 214 | +------------+-------------+------+-----+----------+-------+ 215 | | id | int(11) | NO | PRI | NULL | | 216 | | firstname | varchar(20) | NO | | NULL | | 217 | | middlename | varchar(20) | YES | | NULL | | 218 | | lastname | varchar(20) | NO | | NULL | | 219 | | age | int(11) | NO | | NULL | | 220 | | salary | int(11) | NO | | NULL | | 221 | | location | varchar(20) | NO | | BANGLORE | | 222 | +------------+-------------+------+-----+----------+-------+ 223 | 224 | 225 | alter table employee modify column firstname varchar(30); 226 | 227 | alter table employee modify column firstname varchar(30); 228 | Query OK, 0 rows affected (0.17 sec) 229 | Records: 0 Duplicates: 0 Warnings: 0 230 | 231 | mysql> DESC employee; 232 | +------------+-------------+------+-----+----------+-------+ 233 | | Field | Type | Null | Key | Default | Extra | 234 | +------------+-------------+------+-----+----------+-------+ 235 | | id | int(11) | NO | PRI | NULL | | 236 | | firstname | varchar(30) | YES | | NULL | | 237 | | middlename | varchar(20) | YES | | NULL | | 238 | | lastname | varchar(20) | NO | | NULL | | 239 | | age | int(11) | NO | | NULL | | 240 | | salary | int(11) | NO | | NULL | | 241 | | location | varchar(20) | NO | | BANGLORE | | 242 | +------------+-------------+------+-----+----------+-------+ 243 | 244 | 245 | alter table employee drop primary key; 246 | 247 | 248 | alter table employee drop primary key; 249 | Query OK, 0 rows affected (0.18 sec) 250 | Records: 0 Duplicates: 0 Warnings: 0 251 | 252 | mysql> DESC employee; 253 | +------------+-------------+------+-----+----------+-------+ 254 | | Field | Type | Null | Key | Default | Extra | 255 | +------------+-------------+------+-----+----------+-------+ 256 | | id | int(11) | NO | | NULL | | 257 | | firstname | varchar(30) | YES | | NULL | | 258 | | middlename | varchar(20) | YES | | NULL | | 259 | | lastname | varchar(20) | NO | | NULL | | 260 | | age | int(11) | NO | | NULL | | 261 | | salary | int(11) | NO | | NULL | | 262 | | location | varchar(20) | NO | | BANGLORE | | 263 | +------------+-------------+------+-----+----------+-------+ 264 | 265 | 266 | alter table employee add primary key(id); 267 | 268 | 269 | alter table employee add primary key(id); 270 | Query OK, 0 rows affected (0.11 sec) 271 | Records: 0 Duplicates: 0 Warnings: 0 272 | 273 | mysql> DESC employee; 274 | +------------+-------------+------+-----+----------+-------+ 275 | | Field | Type | Null | Key | Default | Extra | 276 | +------------+-------------+------+-----+----------+-------+ 277 | | id | int(11) | NO | PRI | NULL | | 278 | | firstname | varchar(30) | YES | | NULL | | 279 | | middlename | varchar(20) | YES | | NULL | | 280 | | lastname | varchar(20) | NO | | NULL | | 281 | | age | int(11) | NO | | NULL | | 282 | | salary | int(11) | NO | | NULL | | 283 | | location | varchar(20) | NO | | BANGLORE | | 284 | +------------+-------------+------+-----+----------+-------+ 285 | 286 | 287 | DDL VS DML: 288 | 289 | ================================== 290 | 291 | Data Definition Language: 292 | 293 | Deals with table structure. 294 | 295 | CREATE, ALTER, DROP are DDL commands. 296 | 297 | Data Manipulation Language: 298 | 299 | Deals with data directly. 300 | 301 | INSERT, UPDATE, DELETE are DML commands 302 | 303 | delete from table; 304 | 305 | Truncate also removes all records but it is DDL command. 306 | 307 | When you use delete it will delete the data one by one records 308 | 309 | When we use truncate it drops the table internally and recreate again. 310 | 311 | truncate is more effeciant than delete. 312 | 313 | 314 | 315 | 316 | -------------------------------------------------------------------------------- /SQLNotes/Session5-ForeignKeyConstraint.txt: -------------------------------------------------------------------------------- 1 | SQL Session 5: 2 | 3 | ================================================= 4 | 5 | Foreign Key Constraint; 6 | ==================================== 7 | 8 | Student Table: 9 | =================== 10 | create table students( 11 | student_id INT AUTO_INCREMENT, 12 | student_fname varchar(30) NOT NULL, 13 | student_lname varchar(30) NOT NULL, 14 | student_mname varchar(30), 15 | student_email varchar(30) NOT NULL, 16 | sudent_phone varchar(15) NOT NULL, 17 | student_alternate_phone varchar(15), 18 | enrollment_date TIMESTAMP NOT NULL, 19 | years_of_exp INT NOT NULL, 20 | student_company varchar(30), 21 | batch_date varchar(30) NOT NULL, 22 | source_of_joining varchar(30) NOT NULL, 23 | location varchar(30) NOT NULL, 24 | PRIMARY KEY(student_id), 25 | UNIQUE KEY(student_email) 26 | ); 27 | 28 | 29 | 30 | insert into student(student_fname,student_lname,student_email,sudent_phone,years_of_exp,student_company,batch_date,source_of_joining,location) values('rohit','sharma','rohit@gmail.com','9191919191',6,'walmart','05-02-2021','linkedin','bangalore'); 31 | 32 | insert into students(student_fname,student_lname,student_email,sudent_phone,years_of_exp,student_company,batch_date,source_of_joining,location) values('virat','kohli','virat@gmail.com','9292929292',3,'flipkart','05-02-2021','linkedin','hyderabad'); 33 | 34 | insert into students(student_fname,student_lname,student_email,sudent_phone,years_of_exp,student_company,batch_date,source_of_joining,location) values('shikar','dhawan','shikar@gmail.com','9393939393',12,'19-02-2021','google','bangalore'); 35 | 36 | insert into students(student_fname,student_lname,student_email,sudent_phone,years_of_exp,student_company,batch_date,source_of_joining,location) values('rahul','dravid','rahul@gmail.com','9494949494',8,'walmart','19-02-2021','quora','chennai'); 37 | 38 | insert into students(student_fname,student_lname,student_email,sudent_phone,years_of_exp,student_company,batch_date,source_of_joining,location) values('kapil','dev','kapil@gmail.com','9294919191',15,'microsoft','05-02-2021','friend','pune'),('brain','lara','brain@gmail.com','9394919191',18,'tcs','05-02-2021','youtube','pune'),('carl','hooper','carl@gmail.com','9293519191',20,'wipro','19-02-2021','youtube','pune'),('saurabh','ganguly','saurabh@gmail.com','9291975191',14,'wipro','19-02-2021','google','chennai'); 39 | 40 | 41 | 42 | SELECT Statement; 43 | 44 | ===================== 45 | 46 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students; 47 | 48 | 49 | Courses Table: 50 | 51 | ========================== 52 | 53 | create table courses( 54 | course_id INT NOT NULL, 55 | course_name varchar(30) NOT NULL, 56 | course_duration_in_months INT NOT NULL, 57 | course_fee INT NOT NULL, 58 | PRIMARY KEY(course_id) 59 | ); 60 | 61 | 62 | insert into courses(course_id,course_name,course_duration_in_months,course_fee) 63 | values(1,'bigdata',6,40000), 64 | (2,'webdevelopment',3,20000), 65 | (3,'datascience',6,50000), 66 | (4,'devops',1,10000); 67 | 68 | 69 | Student Table: 70 | =================== 71 | create table students( 72 | student_id INT AUTO_INCREMENT, 73 | student_fname varchar(30) NOT NULL, 74 | student_lname varchar(30) NOT NULL, 75 | student_mname varchar(30), 76 | student_email varchar(30) NOT NULL, 77 | sudent_phone varchar(15) NOT NULL, 78 | student_alternate_phone varchar(15), 79 | enrollment_date TIMESTAMP NOT NULL, 80 | selected_course INT NOT NULL DEFAULT 1, 81 | years_of_exp INT NOT NULL, 82 | student_company varchar(30), 83 | batch_date varchar(30) NOT NULL, 84 | source_of_joining varchar(30) NOT NULL, 85 | location varchar(30) NOT NULL, 86 | PRIMARY KEY(student_id), 87 | UNIQUE KEY(student_email) 88 | ); 89 | 90 | 91 | 92 | 93 | insert into students(student_fname,student_lname,student_email,sudent_phone,selected_course,years_of_exp,student_company,batch_date,source_of_joining,location) values('rohit','sharma','rohit@gmail.com','9191919191',3,6,'walmart','05-02-2021','linkedin','bangalore'); 94 | 95 | insert into students(student_fname,student_lname,student_email,sudent_phone,selected_course,years_of_exp,student_company,batch_date,source_of_joining,location) values('virat','kohli','virat@gmail.com','9292929292',2,'flipkart','05-02-2021','linkedin','hyderabad'); 96 | 97 | insert into students(student_fname,student_lname,student_email,sudent_phone,selected_course,years_of_exp,student_company,batch_date,source_of_joining,location) values('shikar','dhawan','shikar@gmail.com','9393939393',4,12,'19-02-2021','google','bangalore'); 98 | 99 | insert into students(student_fname,student_lname,student_email,sudent_phone,years_of_exp,student_company,batch_date,source_of_joining,location) values('rahul','dravid','rahul@gmail.com','9494949494',8,'walmart','19-02-2021','quora','chennai'); 100 | 101 | insert into students(student_fname,student_lname,student_email,sudent_phone,selected_course,years_of_exp,student_company,batch_date,source_of_joining,location) values('kapil','dev','kapil@gmail.com','9294919191',4,15,'microsoft','05-02-2021','friend','pune'),('brain','lara','brain@gmail.com','9394919191',2,18,'tcs','05-02-2021','youtube','pune'),('carl','hooper','carl@gmail.com','9293519191',1,20,'wipro','19-02-2021','youtube','pune'),('saurabh','ganguly','saurabh@gmail.com','9291975191',3,14,'wipro','19-02-2021','google','chennai'); 102 | 103 | insert into students(student_fname,student_lname,student_email,sudent_phone,selected_course,years_of_exp,student_company,batch_date,source_of_joining,location) values('jasprit','bhumra','jasprit@gmail.com','9191619191',5,6,'walmart','05-02-2021','linkedin','bangalore'); 104 | 105 | 106 | Creating Table with Foreign Key constraint: 107 | ============================================== 108 | 109 | 110 | create table students( 111 | student_id INT AUTO_INCREMENT, 112 | student_fname varchar(30) NOT NULL, 113 | student_lname varchar(30) NOT NULL, 114 | student_mname varchar(30), 115 | student_email varchar(30) NOT NULL, 116 | sudent_phone varchar(15) NOT NULL, 117 | student_alternate_phone varchar(15), 118 | enrollment_date TIMESTAMP NOT NULL, 119 | selected_course INT NOT NULL DEFAULT 1, 120 | years_of_exp INT NOT NULL, 121 | student_company varchar(30), 122 | batch_date varchar(30) NOT NULL, 123 | source_of_joining varchar(30) NOT NULL, 124 | location varchar(30) NOT NULL, 125 | PRIMARY KEY(student_id), 126 | UNIQUE KEY(student_email), 127 | FOREIGN KEY(selected_course) REFERENCES courses(course_id) 128 | ); 129 | 130 | 131 | parent - courses 132 | 133 | child - students 134 | 135 | 136 | The foreign key constraint is used to prevent actions that would destroy links between two tables. 137 | 138 | Fore Key is the field in one table that refers to primary key in other table. 139 | 140 | selected_course is foreign key in students table which refers to course_id in courses table. 141 | 142 | the table with foreign key is called child table and the key with primary key is called reference table or parent table. 143 | 144 | constraint are used to limit the type of data tht can go into table 145 | 146 | this ensures the accuracy and reliability of the data is maintained. 147 | 148 | if there is any violation then the action is aborted. 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /SQLNotes/Session6-DistinctOderbyLimitKeyword.txt: -------------------------------------------------------------------------------- 1 | Session 6: 2 | 3 | ================================= 4 | 5 | Distinct 6 | 7 | ORDER BY 8 | 9 | Limit 10 | 11 | Like 12 | 13 | Distinct: 14 | 15 | =============================== 16 | 17 | 18 | mysql> select distinct(location) from students; 19 | +-----------+ 20 | | location | 21 | +-----------+ 22 | | bangalore | 23 | | chennai | 24 | | pune | 25 | +-----------+ 26 | 3 rows in set (0.00 sec) 27 | 28 | 29 | select distinct(student_company) from students; 30 | +-----------------+ 31 | | student_company | 32 | +-----------------+ 33 | | walmart | 34 | | microsoft | 35 | | tcs | 36 | | wipro | 37 | +-----------------+ 38 | 39 | 40 | select distinct(source_of_joining) from students; 41 | 42 | 43 | select distinct(source_of_joining) from students; 44 | +-------------------+ 45 | | source_of_joining | 46 | +-------------------+ 47 | | linkedin | 48 | | quora | 49 | | friend | 50 | | youtube | 51 | | google | 52 | +-------------------+ 53 | 54 | ORDER BY: 55 | 56 | ==================================== 57 | 58 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students o 59 | rder by years_of_exp;; 60 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+-----------+ 61 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 62 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+-----------+ 63 | | 1 | 2022-07-09 00:03:08 | rohit | rohit@gmail.com | 6 | walmart | 05-02-2021 | linkedin | bangalore | 64 | | 2 | 2022-07-09 00:03:08 | rahul | rahul@gmail.com | 8 | walmart | 19-02-2021 | quora | chennai | 65 | | 6 | 2022-07-09 00:03:09 | saurabh | saurabh@gmail.com | 14 | wipro | 19-02-2021 | google | chennai | 66 | | 3 | 2022-07-09 00:03:09 | kapil | kapil@gmail.com | 15 | microsoft | 05-02-2021 | friend | pune | 67 | | 4 | 2022-07-09 00:03:09 | brain | brain@gmail.com | 18 | tcs | 05-02-2021 | youtube | pune | 68 | | 5 | 2022-07-09 00:03:09 | carl | carl@gmail.com | 20 | wipro | 19-02-2021 | youtube | pune | 69 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+-----------+ 70 | 71 | 72 | 73 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students o 74 | rder by years_of_exp desc; 75 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+-----------+ 76 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 77 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+-----------+ 78 | | 5 | 2022-07-09 00:03:09 | carl | carl@gmail.com | 20 | wipro | 19-02-2021 | youtube | pune | 79 | | 4 | 2022-07-09 00:03:09 | brain | brain@gmail.com | 18 | tcs | 05-02-2021 | youtube | pune | 80 | | 3 | 2022-07-09 00:03:09 | kapil | kapil@gmail.com | 15 | microsoft | 05-02-2021 | friend | pune | 81 | | 6 | 2022-07-09 00:03:09 | saurabh | saurabh@gmail.com | 14 | wipro | 19-02-2021 | google | chennai | 82 | | 2 | 2022-07-09 00:03:08 | rahul | rahul@gmail.com | 8 | walmart | 19-02-2021 | quora | chennai | 83 | | 1 | 2022-07-09 00:03:08 | rohit | rohit@gmail.com | 6 | walmart | 05-02-2021 | linkedin | bangalore | 84 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+-----------+ 85 | 86 | 87 | select student_fname from students order by years_of_exp; 88 | +---------------+ 89 | | student_fname | 90 | +---------------+ 91 | | rohit | 92 | | rahul | 93 | | saurabh | 94 | | kapil | 95 | | brain | 96 | | carl | 97 | +---------------+ 98 | 99 | select student_fname,years_of_exp from students order by years_of_exp; 100 | +---------------+--------------+ 101 | | student_fname | years_of_exp | 102 | +---------------+--------------+ 103 | | rohit | 6 | 104 | | rahul | 8 | 105 | | saurabh | 14 | 106 | | kapil | 15 | 107 | | brain | 18 | 108 | | carl | 20 | 109 | +---------------+--------------+ 110 | 111 | select student_fname,years_of_exp from students order by years_of_exp desc; 112 | +---------------+--------------+ 113 | | student_fname | years_of_exp | 114 | +---------------+--------------+ 115 | | carl | 20 | 116 | | brain | 18 | 117 | | kapil | 15 | 118 | | saurabh | 14 | 119 | | rahul | 8 | 120 | | rohit | 6 | 121 | +---------------+--------------+ 122 | 123 | 124 | select student_fname,years_of_exp from students order by 2 ; 125 | +---------------+--------------+ 126 | | student_fname | years_of_exp | 127 | +---------------+--------------+ 128 | | rohit | 6 | 129 | | rahul | 8 | 130 | | saurabh | 14 | 131 | | kapil | 15 | 132 | | brain | 18 | 133 | | carl | 20 | 134 | +---------------+--------------+ 135 | 6 rows in set (0.00 sec) 136 | 137 | mysql> select student_fname,years_of_exp from students order by 1; 138 | +---------------+--------------+ 139 | | student_fname | years_of_exp | 140 | +---------------+--------------+ 141 | | brain | 18 | 142 | | carl | 20 | 143 | | kapil | 15 | 144 | | rahul | 8 | 145 | | rohit | 6 | 146 | | saurabh | 14 | 147 | +---------------+--------------+ 148 | 149 | 150 | select student_fname,years_of_exp from students order by 1 desc; 151 | +---------------+--------------+ 152 | | student_fname | years_of_exp | 153 | +---------------+--------------+ 154 | | saurabh | 14 | 155 | | rohit | 6 | 156 | | rahul | 8 | 157 | | kapil | 15 | 158 | | carl | 20 | 159 | | brain | 18 | 160 | +---------------+--------------+ 161 | 162 | 163 | 164 | select student_fname,years_of_exp from students order by years_of_exp,student_fname; 165 | +---------------+--------------+ 166 | | student_fname | years_of_exp | 167 | +---------------+--------------+ 168 | | rohit | 6 | 169 | | rahul | 8 | 170 | | saurabh | 14 | 171 | | kapil | 15 | 172 | | brain | 18 | 173 | | carl | 20 | 174 | +---------------+--------------+ 175 | 176 | 177 | LIMIT: 178 | 179 | ============================================== 180 | 181 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students o 182 | rder by years_of_exp limit 3; 183 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+-----------+ 184 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 185 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+-----------+ 186 | | 1 | 2022-07-09 00:03:08 | rohit | rohit@gmail.com | 6 | walmart | 05-02-2021 | linkedin | bangalore | 187 | | 2 | 2022-07-09 00:03:08 | rahul | rahul@gmail.com | 8 | walmart | 19-02-2021 | quora | chennai | 188 | | 6 | 2022-07-09 00:03:09 | saurabh | saurabh@gmail.com | 14 | wipro | 19-02-2021 | google | chennai | 189 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+-----------+ 190 | 191 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students o 192 | rder by years_of_exp desc limit 3;; 193 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+----------+ 194 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 195 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+----------+ 196 | | 5 | 2022-07-09 00:03:09 | carl | carl@gmail.com | 20 | wipro | 19-02-2021 | youtube | pune | 197 | | 4 | 2022-07-09 00:03:09 | brain | brain@gmail.com | 18 | tcs | 05-02-2021 | youtube | pune | 198 | | 3 | 2022-07-09 00:03:09 | kapil | kapil@gmail.com | 15 | microsoft | 05-02-2021 | friend | pune | 199 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+----------+ 200 | 3 rows in set (0.00 sec) 201 | 202 | Want to know which sources last 5 candidates have enrolled. 203 | 204 | 205 | select source_of_joining from students order by enrollment_date desc limit 5; 206 | +-------------------+ 207 | | source_of_joining | 208 | +-------------------+ 209 | | friend | 210 | | youtube | 211 | | youtube | 212 | | google | 213 | | linkedin | 214 | 215 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students o 216 | rder by enrollment_date desc limit 1; 217 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+----------+ 218 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 219 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+----------+ 220 | | 6 | 2022-07-09 00:03:09 | saurabh | saurabh@gmail.com | 14 | wipro | 19-02-2021 | google | chennai | 221 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+----------+ 222 | 223 | 224 | select * from students order by enrollment_date desc limit 0, 3; 225 | +------------+---------------+---------------+---------------+-------------------+--------------+-------------------------+---------------------+------ 226 | -----------+--------------+-----------------+------------+-------------------+----------+ 227 | | student_id | student_fname | student_lname | student_mname | student_email | sudent_phone | student_alternate_phone | enrollment_date | selec 228 | ted_course | years_of_exp | student_company | batch_date | source_of_joining | location | 229 | +------------+---------------+---------------+---------------+-------------------+--------------+-------------------------+---------------------+------ 230 | -----------+--------------+-----------------+------------+-------------------+----------+ 231 | | 6 | saurabh | ganguly | NULL | saurabh@gmail.com | 9291975191 | NULL | 2022-07-09 00:03:09 | 232 | 3 | 14 | wipro | 19-02-2021 | google | chennai | 233 | | 5 | carl | hooper | NULL | carl@gmail.com | 9293519191 | NULL | 2022-07-09 00:03:09 | 234 | 1 | 20 | wipro | 19-02-2021 | youtube | pune | 235 | | 3 | kapil | dev | NULL | kapil@gmail.com | 9294919191 | NULL | 2022-07-09 00:03:09 | 236 | 4 | 15 | microsoft | 05-02-2021 | friend | pune | 237 | +------------+---------------+---------------+---------------+-------------------+--------------+-------------------------+---------------------+------ 238 | -----------+--------------+-----------------+------------+-------------------+----------+ 239 | 3 rows in set (0.00 sec) 240 | 241 | mysql> select * from students order by enrollment_date desc limit 3,2; 242 | +------------+---------------+---------------+---------------+-------------------+--------------+-------------------------+---------------------+------ 243 | -----------+--------------+-----------------+------------+-------------------+-----------+ 244 | | student_id | student_fname | student_lname | student_mname | student_email | sudent_phone | student_alternate_phone | enrollment_date | selec 245 | ted_course | years_of_exp | student_company | batch_date | source_of_joining | location | 246 | +------------+---------------+---------------+---------------+-------------------+--------------+-------------------------+---------------------+------ 247 | -----------+--------------+-----------------+------------+-------------------+-----------+ 248 | | 6 | saurabh | ganguly | NULL | saurabh@gmail.com | 9291975191 | NULL | 2022-07-09 00:03:09 | 249 | 3 | 14 | wipro | 19-02-2021 | google | chennai | 250 | | 1 | rohit | sharma | NULL | rohit@gmail.com | 9191919191 | NULL | 2022-07-09 00:03:08 | 251 | 3 | 6 | walmart | 05-02-2021 | linkedin | bangalore | 252 | +------------+---------------+---------------+---------------+-------------------+--------------+-------------------------+---------------------+------ 253 | -----------+--------------+-----------------+------------+-------------------+-----------+ 254 | 2 rows in set (0.00 sec) 255 | 256 | 257 | LIKE: 258 | 259 | ============================================= 260 | 261 | 262 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students w 263 | here student_fname like '%ra%'; 264 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+----------+ 265 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 266 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+----------+ 267 | | 2 | 2022-07-09 00:03:08 | rahul | rahul@gmail.com | 8 | walmart | 19-02-2021 | quora | chennai | 268 | | 4 | 2022-07-09 00:03:09 | brain | brain@gmail.com | 18 | tcs | 05-02-2021 | youtube | pune | 269 | | 6 | 2022-07-09 00:03:09 | saurabh | saurabh@gmail.com | 14 | wipro | 19-02-2021 | google | chennai | 270 | +------------+---------------------+---------------+-------------------+--------------+-----------------+------------+-------------------+----------+ 271 | 272 | 273 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students w 274 | here student_fname like 'ra%'; 275 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+----------+ 276 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 277 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+----------+ 278 | | 2 | 2022-07-09 00:03:08 | rahul | rahul@gmail.com | 8 | walmart | 19-02-2021 | quora | chennai | 279 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+----------+ 280 | 1 row in set (0.00 sec) 281 | 282 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students w 283 | here student_fname like '%it'; 284 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+-----------+ 285 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 286 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+-----------+ 287 | | 1 | 2022-07-09 00:03:08 | rohit | rohit@gmail.com | 6 | walmart | 05-02-2021 | linkedin | bangalore | 288 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+-----------+ 289 | 1 row in set (0.00 sec) 290 | 291 | 292 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students w 293 | here student_fname like '_____'; 294 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+-----------+ 295 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 296 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+-----------+ 297 | | 1 | 2022-07-09 00:03:08 | rohit | rohit@gmail.com | 6 | walmart | 05-02-2021 | linkedin | bangalore | 298 | | 2 | 2022-07-09 00:03:08 | rahul | rahul@gmail.com | 8 | walmart | 19-02-2021 | quora | chennai | 299 | | 3 | 2022-07-09 00:03:09 | kapil | kapil@gmail.com | 15 | microsoft | 05-02-2021 | friend | pune | 300 | | 4 | 2022-07-09 00:03:09 | brain | brain@gmail.com | 18 | tcs | 05-02-2021 | youtube | pune | 301 | +------------+---------------------+---------------+-----------------+--------------+-----------------+------------+-------------------+-----------+ 302 | 303 | select student_id,enrollment_date,student_fname,student_email,years_of_exp,student_company,batch_date,source_of_joining,location from students w 304 | here student_fname like '____'; 305 | +------------+---------------------+---------------+----------------+--------------+-----------------+------------+-------------------+----------+ 306 | | student_id | enrollment_date | student_fname | student_email | years_of_exp | student_company | batch_date | source_of_joining | location | 307 | +------------+---------------------+---------------+----------------+--------------+-----------------+------------+-------------------+----------+ 308 | | 5 | 2022-07-09 00:03:09 | carl | carl@gmail.com | 20 | wipro | 19-02-2021 | youtube | pune | 309 | +------------+---------------------+---------------+----------------+--------------+-----------------+------------+-------------------+----------+ 310 | 311 | % and _ are wild card characters. 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | -------------------------------------------------------------------------------- /SQLNotes/Session7-OrderOfExecutionSQL.txt: -------------------------------------------------------------------------------- 1 | Session 7: 2 | 3 | ============================= 4 | 5 | select distinct source_of_joing from students order by enrollment_date desc; 6 | 7 | Distinct and Order By in the same Query. 8 | 9 | 10 | case 1: 11 | 12 | select source_of_joing from students 13 | 14 | Order of execution: 15 | ============================= 16 | 17 | 1) From (Loading the Table) 18 | 19 | 2) Select (Projecting source_of_joining) 20 | 21 | case 2: 22 | 23 | select source_of_joing, enrollment_date from students. 24 | 25 | Order of execution: 26 | ============================= 27 | 28 | 1) From (Loading the Table) 29 | 30 | 2) Select (Projecting source_of_joining and source_of_joining) 31 | 32 | case 3: 33 | 34 | select source_of_joing, enrollment_date from students order by enrollment_date; 35 | 36 | Order of execution: 37 | ============================= 38 | 39 | 1) From (Loading the Table) 40 | 41 | 2) Select (Projecting source_of_joining and source_of_joining) 42 | 43 | 3) Order By (based on enrollment_date it will order by) 44 | 45 | 46 | case 4: 47 | 48 | select source_of_joing from students order by enrollment_date; 49 | 50 | 51 | Order of execution: 52 | ============================= 53 | 54 | 1) From (Loading the Table) 55 | 56 | 2) Select (Projecting source_of_joining , enrollment_date) 57 | 58 | 3) Order By (based on enrollment_date it will order by) 59 | 60 | 61 | 62 | case 5: 63 | 64 | select distinct source_of_joining from students order by enrollment_date; 65 | 66 | Order of execution: 67 | ============================= 68 | 69 | 1) From (Loading the Table) 70 | 71 | 2) Select (Projecting source_of_joining , enrollment_date) 72 | 73 | select source_of_joining,enrollment_date from students; 74 | 75 | 3) distinct 76 | 77 | select distinct source_of_joining,enrollment_date from students; 78 | 79 | 3) Order By (based on enrollment_date it will order by) 80 | 81 | -------------------------------------------------------------------------------- /SQLNotes/Session8-AggregateFunctions.txt: -------------------------------------------------------------------------------- 1 | Session 8: 2 | ================================ 3 | 4 | Aggregate Functions: 5 | 6 | ==================================== 7 | 8 | COUNT: 9 | 10 | ========================= 11 | 12 | mysql> select count(*) from students; 13 | +----------+ 14 | | count(*) | 15 | +----------+ 16 | | 6 | 17 | +----------+ 18 | 1 row in set (0.00 sec) 19 | 20 | mysql> select count(DISTINCT srudent_company) from students; 21 | ERROR 1054 (42S22): Unknown column 'srudent_company' in 'field list' 22 | mysql> select count(DISTINCT student_company) from students; 23 | +---------------------------------+ 24 | | count(DISTINCT student_company) | 25 | +---------------------------------+ 26 | | 4 | 27 | +---------------------------------+ 28 | 1 row in set (0.00 sec) 29 | 30 | mysql> select count(DISTINCT student_company) as num_companies from students; 31 | +---------------+ 32 | | num_companies | 33 | +---------------+ 34 | | 4 | 35 | +---------------+ 36 | 1 row in set (0.00 sec) 37 | 38 | 39 | 40 | select count(DISTINCT location) from students; 41 | +--------------------------+ 42 | | count(DISTINCT location) | 43 | +--------------------------+ 44 | | 3 | 45 | +--------------------------+ 46 | 1 row in set (0.00 sec) 47 | 48 | mysql> select count(DISTINCT source_of_joining) from students; 49 | +-----------------------------------+ 50 | | count(DISTINCT source_of_joining) | 51 | +-----------------------------------+ 52 | | 5 | 53 | +-----------------------------------+ 54 | 55 | mysql> select count(*) from students where batch_date like '19%'; 56 | +----------+ 57 | | count(*) | 58 | +----------+ 59 | | 3 | 60 | +----------+ 61 | 1 row in set (0.00 sec) 62 | 63 | 64 | GROUP BY VIMP: 65 | 66 | =========================================== 67 | 68 | Waht I want to use to know how many people joined my course thorugh 69 | 70 | Linked 5 71 | 72 | google 4 73 | 74 | quora 3 75 | 76 | Needs to group by source_of_joining 77 | 78 | 79 | select source_of_joining, count(*) from students group by source_of_joining; 80 | +-------------------+----------+ 81 | | source_of_joining | count(*) | 82 | +-------------------+----------+ 83 | | friend | 1 | 84 | | google | 1 | 85 | | linkedin | 1 | 86 | | quora | 1 | 87 | | youtube | 2 | 88 | +-------------------+----------+ 89 | 90 | select location, count(*) from students group by location; 91 | +-----------+----------+ 92 | | location | count(*) | 93 | +-----------+----------+ 94 | | bangalore | 1 | 95 | | chennai | 2 | 96 | | pune | 3 | 97 | +-----------+----------+ 98 | 3 rows in set (0.00 sec) 99 | 100 | 101 | 102 | select location, count(*) from students group by source_of_joining; This is will not work. 103 | 104 | 105 | select location,source_of_joining,count(*) from students group by location,source_of_joining; 106 | +-----------+-------------------+----------+ 107 | | location | source_of_joining | count(*) | 108 | +-----------+-------------------+----------+ 109 | | bangalore | linkedin | 1 | 110 | | chennai | google | 1 | 111 | | chennai | quora | 1 | 112 | | pune | friend | 1 | 113 | | pune | youtube | 2 | 114 | +-----------+-------------------+----------+ 115 | 116 | select selected_course, count(*) from students GROUP BY selected_course; 117 | +-----------------+----------+ 118 | | selected_course | count(*) | 119 | +-----------------+----------+ 120 | | 1 | 2 | 121 | | 2 | 1 | 122 | | 3 | 2 | 123 | | 4 | 1 | 124 | +-----------------+----------+ 125 | 126 | select batch_date,selected_course,count(*) from students GROUP BY batch_date,selected_course; 127 | +------------+-----------------+----------+ 128 | | batch_date | selected_course | count(*) | 129 | +------------+-----------------+----------+ 130 | | 05-02-2021 | 2 | 1 | 131 | | 05-02-2021 | 3 | 1 | 132 | | 05-02-2021 | 4 | 1 | 133 | | 19-02-2021 | 1 | 2 | 134 | | 19-02-2021 | 3 | 1 | 135 | +------------+-----------------+----------+ 136 | 137 | 138 | MIN & MAX: 139 | 140 | =========================== 141 | 142 | select MIN(years_of_exp) from students; 143 | +-------------------+ 144 | | MIN(years_of_exp) | 145 | +-------------------+ 146 | | 6 | 147 | +-------------------+ 148 | 1 row in set (0.00 sec) 149 | 150 | mysql> select MAX(years_of_exp) from students; 151 | +-------------------+ 152 | | MAX(years_of_exp) | 153 | +-------------------+ 154 | | 20 | 155 | +-------------------+ 156 | 1 row in set (0.00 sec) 157 | 158 | 159 | Each Source Of joining I want to get max : 160 | 161 | select source_of_joining,max(years_of_exp) from students group by source_of_joining; 162 | +-------------------+-------------------+ 163 | | source_of_joining | max(years_of_exp) | 164 | +-------------------+-------------------+ 165 | | friend | 15 | 166 | | google | 14 | 167 | | linkedin | 6 | 168 | | quora | 8 | 169 | | youtube | 20 | 170 | +-------------------+-------------------+ 171 | 172 | SUM: 173 | 174 | ===================================== 175 | 176 | select source_of_joining, sum(years_of_exp) from students group by source_of_joining; 177 | +-------------------+-------------------+ 178 | | source_of_joining | sum(years_of_exp) | 179 | +-------------------+-------------------+ 180 | | friend | 15 | 181 | | google | 14 | 182 | | linkedin | 6 | 183 | | quora | 8 | 184 | | youtube | 38 | 185 | +-------------------+-------------------+ 186 | 187 | AVG: 188 | 189 | =============================== 190 | 191 | select source_of_joining, avg(years_of_exp) from students group by source_of_joining; 192 | +-------------------+-------------------+ 193 | | source_of_joining | avg(years_of_exp) | 194 | +-------------------+-------------------+ 195 | | friend | 15.0000 | 196 | | google | 14.0000 | 197 | | linkedin | 6.0000 | 198 | | quora | 8.0000 | 199 | | youtube | 19.0000 | 200 | +-------------------+-------------------+ 201 | 202 | select location, avg(years_of_exp) from students group by location; 203 | +-----------+-------------------+ 204 | | location | avg(years_of_exp) | 205 | +-----------+-------------------+ 206 | | bangalore | 6.0000 | 207 | | chennai | 11.0000 | 208 | | pune | 17.6667 | 209 | +-----------+-------------------+ 210 | 211 | 212 | select student_company, avg(years_of_exp) from students group by student_company; 213 | 214 | select student_company, avg(years_of_exp) from students group by student_company; 215 | +-----------------+-------------------+ 216 | | student_company | avg(years_of_exp) | 217 | +-----------------+-------------------+ 218 | | microsoft | 15.0000 | 219 | | tcs | 18.0000 | 220 | | walmart | 7.0000 | 221 | | wipro | 17.0000 | 222 | +-----------------+-------------------+ 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | -------------------------------------------------------------------------------- /SQLNotes/Session9-FewMoreDatatypes.txt: -------------------------------------------------------------------------------- 1 | Session 9: 2 | =============================================== 3 | 4 | create table courses_new( 5 | course_id int AUTO_INCREMENT, 6 | course_name varchar(30) NOT NULL, 7 | course_duration_months decimal(3,1) NOT NULL, 8 | course_fee int NOT NULL, 9 | PRIMARY KEY(course_id) 10 | ); 11 | 12 | insert into courses_new(course_name,course_duration_months,course_fee) values('bigdata', 6.5, 50000),('webdevelopment', 3.5, 20000),('datascience', 6, 40000),; 13 | 14 | insert into courses_new(course_name,course_duration_months,course_fee) values('devops', 10.5, 30000); 15 | 16 | update courses_new set course_fee = 40000 where course_id = 2; 17 | Query OK, 1 row affected (0.00 sec) 18 | Rows matched: 1 Changed: 1 Warnings: 0 19 | 20 | mysql> select * from courses_new; 21 | +-----------+----------------+------------------------+------------+ 22 | | course_id | course_name | course_duration_months | course_fee | 23 | +-----------+----------------+------------------------+------------+ 24 | | 1 | bigdata | 6.5 | 50000 | 25 | | 2 | webdevelopment | 3.5 | 40000 | 26 | | 3 | datascience | 6.0 | 40000 | 27 | +-----------+----------------+------------------------+------------+ 28 | 3 rows in set (0.00 sec) 29 | 30 | create table courses_new( 31 | course_id int AUTO_INCREMENT, 32 | course_name varchar(30) NOT NULL, 33 | course_duration_months decimal(3,1) NOT NULL, 34 | course_fee int NOT NULL, 35 | changed_at TIMESTAMP DEFAULT NOW(), 36 | PRIMARY KEY(course_id) 37 | ); 38 | 39 | mysql> update courses_new set course_duration_months = 4.5 where course_id = 3; 40 | Query OK, 1 row affected (0.01 sec) 41 | Rows matched: 1 Changed: 1 Warnings: 0 42 | 43 | mysql> select * from courses_new; 44 | +-----------+----------------+------------------------+------------+---------------------+ 45 | | course_id | course_name | course_duration_months | course_fee | changed_at | 46 | +-----------+----------------+------------------------+------------+---------------------+ 47 | | 1 | bigdata | 6.5 | 50000 | 2022-07-11 00:01:52 | 48 | | 2 | webdevelopment | 3.5 | 20000 | 2022-07-11 00:01:52 | 49 | | 3 | datascience | 4.5 | 40000 | 2022-07-11 00:01:52 | 50 | +-----------+----------------+------------------------+------------+---------------------+ 51 | 3 rows in set (0.00 sec) 52 | 53 | create table courses_new( 54 | course_id int AUTO_INCREMENT, 55 | course_name varchar(30) NOT NULL, 56 | course_duration_months decimal(3,1) NOT NULL, 57 | course_fee int NOT NULL, 58 | changed_at TIMESTAMP DEFAULT NOW() ON UPDATE CURRENT_TIMESTAMP(), 59 | PRIMARY KEY(course_id) 60 | ); 61 | 62 | update courses_new set course_fee = 40000 where course_id = 2; 63 | Query OK, 1 row affected (0.00 sec) 64 | Rows matched: 1 Changed: 1 Warnings: 0 65 | 66 | mysql> select * from courses_new; 67 | +-----------+----------------+------------------------+------------+---------------------+ 68 | | course_id | course_name | course_duration_months | course_fee | changed_at | 69 | +-----------+----------------+------------------------+------------+---------------------+ 70 | | 1 | devops | 10.5 | 30000 | 2022-07-11 00:08:30 | 71 | | 2 | bigdata | 6.5 | 40000 | 2022-07-11 00:09:13 | 72 | | 3 | webdevelopment | 3.5 | 20000 | 2022-07-11 00:08:58 | 73 | | 4 | datascience | 6.0 | 40000 | 2022-07-11 00:08:58 | 74 | +-----------+----------------+------------------------+------------+---------------------+ 75 | 4 rows in set (0.00 sec) 76 | 77 | DECIMAL(5,3) 78 | 79 | TIMESTAMP CURRENT_TIMESTAMP -------------------------------------------------------------------------------- /SQLNotes/readme: -------------------------------------------------------------------------------- 1 | This is a readme file. 2 | 3 | Added a new feature called as Feature1 4 | 5 | --------------------------------------------------------------------------------