|
1 |
| -/*Description-All the queries mentioned below have been written using a SQL Server Management Studio (SSMS) GUI provided by Microsoft and is platform dependent. Queries are executed using SQL server 2019 Developer edition. We create our own database here using basic sql commands and perform different queries over it to obtain various results. However you can use other different RDBMS such as SQLITE,MySQL,PostgreSQL,ORACLE. The syntax may change slightly although the basics SQL standards are mostly maintained. |
| 1 | +Description-All the queries mentioned below have been written using a SQL Server Management Studio (SSMS) GUI provided by Microsoft and is platform dependent. Queries are executed using SQL server 2019 Developer edition. We create our own database here using basic sql commands and perform different queries over it to obtain various results. However you can use other different RDBMS such as SQLITE,MySQL,PostgreSQL,ORACLE. The syntax may change slightly although the basics SQL standards are mostly maintained. |
2 | 2 |
|
3 | 3 | Link to downnload SQL SERVER - https://www.microsoft.com/en-gb/sql-server/sql-server-downloads
|
4 | 4 |
|
5 | 5 | Tutorial on how to download and install SQL server on windows-
|
6 | 6 | https://www.youtube.com/watch?v=yasfZuou3zI&fbclid=IwAR3WW3_P7byXV7R-ig35QLw7FdlpirvRE9rp8EGXnBh6SIUd-hCpsXcuGxg
|
7 | 7 |
|
8 | 8 |
|
9 |
| -Lets Begin */ |
10 |
| - |
11 |
| ---- Creating A Database |
12 |
| - |
13 |
| -Create Database SampleGitHub |
14 |
| - |
15 |
| ---- Creating a table inside above database and creating columns |
16 |
| - |
17 |
| -create table Customer |
18 |
| -( CustomerName varchar(20), |
19 |
| - CustomerSurname varchar (20), |
20 |
| - Age int |
21 |
| - ) |
22 |
| - |
23 |
| - |
24 |
| - ---Inserting values inside a table |
25 |
| - |
26 |
| - insert into Customer (CustomerName,CustomerSurname,Age) |
27 |
| - VALUES ( 'shantanu','jadhav',30); |
28 |
| - insert into Customer (CustomerName,CustomerSurname,Age) |
29 |
| - VALUES ( 'swapnil','bagul',28); |
30 |
| - insert into Customer (CustomerName,CustomerSurname,Age) |
31 |
| - VALUES ( 'kruz','aher',31); |
32 |
| -insert into Customer (CustomerName,CustomerSurname,Age) |
33 |
| - VALUES ( 'Amol','Harde',31); |
34 |
| -insert into Customer (CustomerName,CustomerSurname,Age) |
35 |
| - VALUES ( 'pratik','phadtare',25) |
36 |
| - |
37 |
| - |
38 |
| - select * from Customer /* Displays complete Customer table */ |
39 |
| - |
40 |
| - select CustomerName,Age from Customer /* Displays only perticular columns from the table*/ |
41 |
| - |
42 |
| - |
43 |
| - |
44 |
| - --- Using where clause to display perticular rows |
45 |
| - |
46 |
| - |
47 |
| - |
48 |
| - select * from customer |
49 |
| - where CustomerName='Amol' |
50 |
| - |
51 |
| - --- Using AND/OR Operator along with where clause |
52 |
| - |
53 |
| - select * from customer |
54 |
| - where CustomerName='shantanu' /* AND OPERATOR */ |
55 |
| - and age=30 |
56 |
| - |
57 |
| - select * from customer |
58 |
| - where CustomerName ='swapnil' /* OR OPERATOR */ |
59 |
| - or age ='31' |
60 |
| - |
61 |
| - ---Using LIKE OPERATOR |
62 |
| - select * from customer /* LIKE OPERATOR */ |
63 |
| - where CustomerSurname like 'a%' /* selects Customers with surname starting with 'a' */ |
64 |
| - |
65 |
| - ---Using Like and '-' operators |
66 |
| - select * from Customer |
67 |
| - where CustomerSurname like '_a%' /* selects surnames having 2nd character as 'a' */ |
68 |
| - |
69 |
| - |
70 |
| - ---Using UPDATE commands for Updating rows and changing values in columns |
71 |
| - |
72 |
| - Update Customer |
73 |
| - set age=31 |
74 |
| - where CustomerName = 'shantanu' |
75 |
| - |
76 |
| - ---Deleting complete data from the table |
77 |
| - |
78 |
| - Delete Customer |
79 |
| - |
80 |
| - ---Deleting a particular row/rows from the table |
81 |
| - Delete from Customer |
82 |
| - where CustomerName= 'shantanu' |
83 |
| - |
84 |
| - ---Deleting whole table |
85 |
| - Drop table Customer |
86 |
| - |
87 |
| - --Altering table/ adding one or more new columns wo the table |
88 |
| - |
89 |
| - Alter table Customer |
90 |
| - add City varchar (20) |
91 |
| - |
92 |
| - select * from Customer |
93 |
| - |
94 |
| - UPDATE customer SET City = 'Mumbai' where CustomerName='shantanu' |
95 |
| - |
96 |
| - /* places city as 'Mumbai' in the all the null values inside city column */ |
97 |
| - |
98 |
| - UPDATE customer SET City = 'nashik' where CustomerName='swapnil' |
99 |
| - |
100 |
| - UPDATE customer SET City = 'Mumbai' where CustomerName='kruz' |
101 |
| - |
102 |
| - UPDATE customer SET City = 'nagpur' where CustomerName='amol' |
103 |
| - |
104 |
| - UPDATE customer SET City = 'thane' where CustomerName='pratik' |
105 |
| - |
106 |
| - select * from Customer |
107 |
| - |
108 |
| - /*Adding Primary Key to the current table*/ |
109 |
| - |
110 |
| - |
111 |
| - Alter table customer |
112 |
| - add CustomerID int primary key identity (1,1) |
113 |
| - |
114 |
| - |
115 |
| - -- Creating PRODUCT and ORDERS table |
116 |
| - |
117 |
| - |
118 |
| - CREATE TABLE PRODUCT |
119 |
| - ( |
120 |
| - ProductID int primary key identity (1,1), |
121 |
| - productname varchar (20), |
122 |
| - productprice int |
123 |
| - ) |
124 |
| - |
125 |
| - insert into PRODUCT (productname,productprice) |
126 |
| - values ('bat',30); |
127 |
| - insert into PRODUCT (productname,productprice) |
128 |
| - values ('ball',10); |
129 |
| - insert into PRODUCT (productname,productprice) |
130 |
| - values ('stumps',3300); |
131 |
| - insert into PRODUCT (productname,productprice) |
132 |
| - values ('gloves',80); |
133 |
| - insert into PRODUCT (productname,productprice) |
134 |
| - values ('shoes',400) |
135 |
| - |
136 |
| - |
137 |
| -select * from PRODUCT |
138 |
| - |
139 |
| - |
140 |
| - |
141 |
| -create table orders |
142 |
| -( |
143 |
| -OrderID int primary key identity (1,1), |
144 |
| -OrderDate datetime, |
145 |
| -C_ID int, |
146 |
| -P_ID int |
147 |
| -) |
148 |
| - |
149 |
| - |
150 |
| -select * from orders |
151 |
| -select * from PRODUCT |
152 |
| -select * from customer |
153 |
| - |
154 |
| - |
155 |
| ----creating relationship between all 3 tables above i.e. Creating Foreign Key |
156 |
| - |
157 |
| - alter table orders |
158 |
| - add foreign key (C_ID) references customer (CustomerID) |
159 |
| - /* Creates Foreign key in the orders table with reference to Customer table primary key*/ |
160 |
| - |
161 |
| - alter table orders |
162 |
| - add foreign key (P_ID) references PRODUCT (ProductID) |
163 |
| - /* Creates Foreign key in the orders table with reference to Product table primary key*/ |
164 |
| - |
165 |
| - insert into orders (OrderDate,C_ID,P_ID) |
166 |
| - values (GetDate(),2,3); |
167 |
| - insert into orders (OrderDate,C_ID,P_ID) |
168 |
| - values (GetDate(),1,4); |
169 |
| -insert into orders (OrderDate,C_ID,P_ID) |
170 |
| - values (GetDate(),3,2); |
171 |
| - insert into orders (OrderDate,C_ID,P_ID) |
172 |
| - values (GetDate(),5,4); |
173 |
| - insert into orders (OrderDate,C_ID,P_ID) |
174 |
| - values (GetDate(),5,1) |
175 |
| - |
176 |
| - |
177 |
| - |
178 |
| - |
179 |
| - |
180 |
| ----Creating JOINS between above 3 tables |
181 |
| - |
182 |
| -select * from orders |
183 |
| -inner join PRODUCT on orders.P_ID=PRODUCT.ProductID |
184 |
| -INNER JOIN customer on orders.C_ID=customer.customerID |
185 |
| - |
186 |
| - |
187 |
| - |
188 |
| --- writing same above code using alias and selecting particular columns |
189 |
| - |
190 |
| - |
191 |
| -select O.orderDate,C.CustomerName,P.productname,P.productprice |
192 |
| -from orders as O inner join PRODUCT as P on O.P_ID=P.ProductID |
193 |
| -INNER JOIN customer AS C on O.C_ID=C.customerID |
194 |
| - |
195 |
| - |
196 |
| - |
197 |
| - |
198 |
| ---Trying out above table with few aggregate functions and group by |
199 |
| - |
200 |
| -select C.CustomerName,sum(P.productprice)as total /* Using aggregate function 'SUM','AVG' */ |
201 |
| -from orders as O inner join PRODUCT as P on O.P_ID=P.ProductID |
202 |
| -INNER JOIN customer AS C on O.C_ID=C.customerID |
203 |
| -GROUP BY CustomerName |
204 |
| - |
205 |
| -select P.productname,AVG(P.productprice)as total /* Using aggregate function 'SUM','AVG' */ |
206 |
| -from orders as O inner join PRODUCT as P on O.P_ID=P.ProductID |
207 |
| -INNER JOIN customer AS C on O.C_ID=C.customerID |
208 |
| -GROUP BY productname |
209 |
| - |
210 |
| - |
211 |
| - |
212 |
| - |
213 |
| - |
| 9 | +Lets Begin |
214 | 10 |
|
0 commit comments