|
| 1 | +ddl-drop,rename,create,alter,truncate |
| 2 | +dml-delete ,insert, update |
| 3 | +dcl-grant,revoke |
| 4 | +dql-select |
| 5 | +tcl-commit,rollback,save point |
| 6 | + |
| 7 | +Q. DIFFERENCE between delete ,truncate,drop |
| 8 | +Q. difference between where and having |
| 9 | +Q. difference between group by and order by |
| 10 | + |
| 11 | +LEVEL 2 |
| 12 | +Q. difference between CHAR AND varchar |
| 13 | +Q. difference between union and join |
| 14 | +Q. difference between in & exist |
| 15 | +Q. how to create empty table with the same structure as another table |
| 16 | +select * INTO students_copy from students where 1=2(false conditon 1=2) |
| 17 | +Q what is pattern matching in sql |
| 18 | +select * form students where name LIKE '_k%'; --SECOND LETTER IS K,% MEANS ANTYHING AFTER THAT |
| 19 | +Q. WHAT IS UNION MINUS INTERSECT |
| 20 | +union -combines, intersect- means common between both tables will be printed,minus all records from student table which are not in contacts table will be printed |
| 21 | +--select name from students UNION select name from contacts |
| 22 | + |
| 23 | +Q difference between union and union all |
| 24 | + union print only once, union all can print duplicates |
| 25 | + |
| 26 | +Q what is a view |
| 27 | +virtual table : from customers where country='India'; |
| 28 | + |
| 29 | +Q character manipulation function |
| 30 | +UPPER,LOWER,INITCAP,LENGTH,CONCAT |
| 31 | + |
| 32 | +level 3 |
| 33 | +NORMAL FOEMS |
| 34 | + |
| 35 | +1. DIFFERENCE BETWEEN WHERE v/s HAVING |
| 36 | +1.WHERE |
| 37 | +-filter rows |
| 38 | +-works on rows data ,not on aggregated data |
| 39 | +select * from employee here score>40; |
| 40 | +2.HAVING |
| 41 | +-works on aggregated data |
| 42 | +AGGREGATE FUNCTIONS |
| 43 | +- TO perform calculation on multiple rows of a single column |
| 44 | +-it returns a single value |
| 45 | +-it is used to summarize data |
| 46 | +-count,max,min,avg,summarize |
| 47 | +-having and group by used in agggregate function |
| 48 | +select max(salary) from employee having emp_id>=15 |
| 49 | + |
| 50 | +2. DIFFERENCE BETWEEN UNION v/s UNION ALL |
| 51 | +UNION - REMOVES duplicate records |
| 52 | +UNION ALLL- does not remove duplicate |
| 53 | +--union operator combines rsult set of 2 or more select statement |
| 54 | +--each select stmt must have |
| 55 | +1.same number coln |
| 56 | +2.coln must have similar data type |
| 57 | +3.coln must be in same order |
| 58 | + |
| 59 | +3. DIFFERENCE BETWEEN IN v/s EXIST |
| 60 | +1.IN -> multiple or |
| 61 | +select * from customers where city='mumbai' or city='banglore' or city='chennai' |
| 62 | +can be converted into like this querry |
| 63 | +select * from customers where city IN (MUMBAI,BANGLORE,CHENNAI); |
| 64 | +2.EXIST-> returns true or false |
| 65 | +select * from customers where EXISTS (select city from where table2.id=customer.id) |
| 66 | +USECASE |
| 67 | +--IN-> bIG OUTER QUERRY AND SMALL INNER QUERRY |
| 68 | +--EXISTS-> SMALL OUTER QUERRY and big inner querry |
| 69 | +--IN-> Compare one value to several value |
| 70 | +--EXISTS->tells you wheather a querry returned any result |
| 71 | + |
| 72 | +4. DIFFERENCE BETWEEN ORDER BY v/s GROUP BY |
| 73 | +ORDER BY-> SORTING,ASC OR DSC |
| 74 | +GROUP BY-> USED WITH AGGREGATE FUNCTIONS |
| 75 | +*'GROUP' BY FOLLOWS 'where' clause in 'select' statement |
| 76 | +*'WHERE' Cannot be used after group by |
| 77 | +*'HAVING' IS USED AFTER group by |
| 78 | + |
| 79 | +4. DIFFERENCE BETWEEN JOIN v/s SUBQUERY |
| 80 | +-both are used to combine data from different tables into a single result |
| 81 | +---SUBQUERRY |
| 82 | +SELECT phone, cust_name from customers where cust_id IN |
| 83 | +(select cust_id from orders); |
| 84 | +-can select only from first table |
| 85 | +-slower |
| 86 | +---JOIN |
| 87 | +SELECT phone, cust_name,ORDER_ID from customers c JOIN orders o on c.cust_id=o.cust_id; |
| 88 | +-can select form either of the table |
| 89 | +-faster |
| 90 | + |
| 91 | +4. DIFFERENCE BETWEEN JOIN v/s UNION |
| 92 | +---union |
| 93 | +-cobines rows |
| 94 | +-not neccessary to have same column name |
| 95 | +-but no. of column & datatype of column should be same |
| 96 | +---JOIN |
| 97 | +-MERGES column |
| 98 | +-combines rows from 2 or more tables based on a related common column between them |
| 99 | + |
| 100 | +Q. how to delete duplicate rows from a table in SQL. |
| 101 | +4 following ways are shown: |
| 102 | +1) remove duplicate rows using a temporary table |
| 103 | +2) remove duplicate rows using auto ID |
| 104 | +3) remove duplicate rows using row_number() |
| 105 | +4) remove duplicate rows using CTE |
| 106 | + |
| 107 | +1.USING TEMP TABLE |
| 108 | +-SELECT distinct * INTO NEW_TABLE FROM OLD_TABLE |
| 109 | +-DELETE * FORM OLD_TABLE or TRUNCATE |
| 110 | +-INSERT INTO OLD_TABLE SELECT * FROM NEW_TABLE |
| 111 | +-DROP TABLE NEW_TABLE |
| 112 | + |
| 113 | +2. USING AUTO_ID |
| 114 | +-ALTER TABLE EMPLOYEE ADD auto_id int IDENTITY(1,1) |
| 115 | +-delete 8 from employee where auto_id NOT IN (SELECT min(auto_id) |
| 116 | +from employee group by emp_id ,emp_name) |
| 117 | + |
| 118 | +3.using row number |
| 119 | +delete from (select 8, row_number() OVER(PARTITION BY ID Order by id) as rn from mytable) |
| 120 | + where rn>1 |
| 121 | + |
| 122 | +4.USING CTE (COMMON TABLE EXPRESSION) |
| 123 | +-used as a temporary result set |
| 124 | +-lasts only for duration for the querry |
| 125 | + |
| 126 | +WITH cte as (select row_number() over(partition by id order by id) as rn from mytable) |
| 127 | +delete from cte rn>1 |
| 128 | + |
| 129 | +5. DIFFERENCE BETWEEN DELETE v/s TRUNCATE |
| 130 | +---DELETE |
| 131 | +-DML ,DELETE REMOVEs some or all rows |
| 132 | +-it does not free space containing the table |
| 133 | +-the transaction log will still have the deleted rows |
| 134 | +-slower than truncate |
| 135 | +---TRUNCATE |
| 136 | +-DDL,removes all the rows from the table |
| 137 | +-it frees up space containing the table |
| 138 | +-no where clause can be used |
| 139 | +---DROP |
| 140 | +-removes table from the database |
| 141 | +-cannot be rolled up |
| 142 | +#Q1.increase income of all employees by 5% in a table |
| 143 | +update employees SET income=income+(income * 5.0/100.0); |
| 144 | + |
| 145 | + |
| 146 | +#find names of employees starting with 'A' |
| 147 | +select first_name fro employees where first_name LIKE 'A%'; |
| 148 | + |
| 149 | +#Q3. find no. of employees working in department 'ABC' |
| 150 | +SELECT COUNT FROM EMPLOYEES WHERE department_name='ABC'; |
| 151 | + |
| 152 | +#Q4. PRINT DETAILS OF EMPLOYEES WHOSE FIRST_NAME END WITH 'A' AND CONTAINS 6 alphabets |
| 153 | +select * from employees where first_name LIKE '______A' |
| 154 | + |
| 155 | +#Q5.print details of employees whose salary lies between 10000 and 50000 |
| 156 | +SELECT * FROM EMPLOYEES WHERE SALARY BETWEEN 10000 AND 50000 |
| 157 | + |
| 158 | +Q8.difference BETWEEN CHAR V/S VARCHAR |
| 159 | +VARCHAR->variable length |
| 160 | +char->fixed length |
| 161 | +---char |
| 162 | +-uses stTIC MEMORY ALLOCATION |
| 163 | +-FIXED length |
| 164 | +-faster |
| 165 | +---VARCHAR |
| 166 | +-it uses dynamic memory location |
| 167 | +-variable length |
| 168 | +-better for storage |
| 169 | +-slower |
| 170 | +-has an overhead of 2 bytes |
| 171 | +---char->telephone no,zip code, etc |
| 172 | +---varchar->names |
| 173 | +Q. fetch to n recordsselec top n* from employees order by salary desc |
| 174 | + |
| 175 | +Q. retrive empfname,and emplname in a single column as 'fullname', first name ,last name |
| 176 | +seprated with space |
| 177 | +--select CONCAT(empfname,' ',emplname) as 'Fullname' from employees; |
0 commit comments