Skip to content

Commit c752a08

Browse files
authored
Update README.md
1 parent cc55ad4 commit c752a08

File tree

1 file changed

+174
-4
lines changed

1 file changed

+174
-4
lines changed

README.md

Lines changed: 174 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
# SQLBasics
2-
--- Creating A Database---
1+
--- Creating A Database
32

43
Create Database SampleGitHub
54

6-
--- Creating a table inside above database and creating columns--
5+
--- Creating a table inside above database and creating columns
76

87
create table Customer
98
( CustomerName varchar(20),
@@ -12,7 +11,7 @@ create table Customer
1211
)
1312

1413

15-
---Inserting values inside a table--
14+
---Inserting values inside a table
1615

1716
insert into Customer (CustomerName,CustomerSurname,Age)
1817
VALUES ( 'shantanu','jadhav',30);
@@ -32,3 +31,174 @@ insert into Customer (CustomerName,CustomerSurname,Age)
3231

3332

3433

34+
--- Using where clause to display perticular rows
35+
36+
37+
38+
select * from customer
39+
where CustomerName='Amol'
40+
41+
--- Using AND/OR Operator along with where clause
42+
43+
select * from customer
44+
where CustomerName='shantanu' /* AND OPERATOR */
45+
and age=30
46+
47+
select * from customer
48+
where CustomerName ='swapnil' /* OR OPERATOR */
49+
or age ='31'
50+
51+
---Using LIKE OPERATOR
52+
select * from customer /* LIKE OPERATOR */
53+
where CustomerSurname like 'a%' /* selects Customers with surname starting with 'a' */
54+
55+
---Using Like and '-' operators
56+
select * from Customer
57+
where CustomerSurname like '_a%' /* selects surnames having 2nd character as 'a' */
58+
59+
60+
---Using UPDATE commands for Updating rows and changing values in columns
61+
62+
Update Customer
63+
set age=31
64+
where CustomerName = 'shantanu'
65+
66+
---Deleting complete data from the table
67+
68+
Delete Customer
69+
70+
---Deleting a particular row/rows from the table
71+
Delete from Customer
72+
where CustomerName= 'shantanu'
73+
74+
---Deleting whole table
75+
Drop table Customer
76+
77+
--Altering table/ adding one or more new columns wo the table
78+
79+
Alter table Customer
80+
add City varchar (20)
81+
82+
select * from Customer
83+
84+
UPDATE customer SET City = 'Mumbai' where CustomerName='shantanu'
85+
86+
/* places city as 'Mumbai' in the all the null values inside city column */
87+
88+
UPDATE customer SET City = 'nashik' where CustomerName='swapnil'
89+
90+
UPDATE customer SET City = 'Mumbai' where CustomerName='kruz'
91+
92+
UPDATE customer SET City = 'nagpur' where CustomerName='amol'
93+
94+
UPDATE customer SET City = 'thane' where CustomerName='pratik'
95+
96+
select * from Customer
97+
98+
/*Adding Primary Key to the current table*/
99+
100+
101+
Alter table customer
102+
add CustomerID int primary key identity (1,1)
103+
104+
105+
-- Creating PRODUCT and ORDERS table
106+
107+
108+
CREATE TABLE PRODUCT
109+
(
110+
ProductID int primary key identity (1,1),
111+
productname varchar (20),
112+
productprice int
113+
)
114+
115+
insert into PRODUCT (productname,productprice)
116+
values ('bat',30);
117+
insert into PRODUCT (productname,productprice)
118+
values ('ball',10);
119+
insert into PRODUCT (productname,productprice)
120+
values ('stumps',3300);
121+
insert into PRODUCT (productname,productprice)
122+
values ('gloves',80);
123+
insert into PRODUCT (productname,productprice)
124+
values ('shoes',400)
125+
126+
127+
select * from PRODUCT
128+
129+
130+
131+
create table orders
132+
(
133+
OrderID int primary key identity (1,1),
134+
OrderDate datetime,
135+
C_ID int,
136+
P_ID int
137+
)
138+
139+
140+
select * from orders
141+
select * from PRODUCT
142+
select * from customer
143+
144+
145+
---creating relationship between all 3 tables above i.e. Creating Foreign Key
146+
147+
alter table orders
148+
add foreign key (C_ID) references customer (CustomerID)
149+
/* Creates Foreign key in the orders table with reference to Customer table primary key*/
150+
151+
alter table orders
152+
add foreign key (P_ID) references PRODUCT (ProductID)
153+
/* Creates Foreign key in the orders table with reference to Product table primary key*/
154+
155+
insert into orders (OrderDate,C_ID,P_ID)
156+
values (GetDate(),2,3);
157+
insert into orders (OrderDate,C_ID,P_ID)
158+
values (GetDate(),1,4);
159+
insert into orders (OrderDate,C_ID,P_ID)
160+
values (GetDate(),3,2);
161+
insert into orders (OrderDate,C_ID,P_ID)
162+
values (GetDate(),5,4);
163+
insert into orders (OrderDate,C_ID,P_ID)
164+
values (GetDate(),5,1)
165+
166+
167+
168+
169+
170+
---Creating JOINS between above 3 tables
171+
172+
select * from orders
173+
inner join PRODUCT on orders.P_ID=PRODUCT.ProductID
174+
INNER JOIN customer on orders.C_ID=customer.customerID
175+
176+
177+
178+
-- writing same above code using alias and selecting particular columns
179+
180+
181+
select O.orderDate,C.CustomerName,P.productname,P.productprice
182+
from orders as O inner join PRODUCT as P on O.P_ID=P.ProductID
183+
INNER JOIN customer AS C on O.C_ID=C.customerID
184+
185+
186+
187+
188+
--Trying out above table with few aggregate functions and group by
189+
190+
select C.CustomerName,sum(P.productprice)as total /* Using aggregate function 'SUM','AVG' */
191+
from orders as O inner join PRODUCT as P on O.P_ID=P.ProductID
192+
INNER JOIN customer AS C on O.C_ID=C.customerID
193+
GROUP BY CustomerName
194+
195+
select P.productname,AVG(P.productprice)as total /* Using aggregate function 'SUM','AVG' */
196+
from orders as O inner join PRODUCT as P on O.P_ID=P.ProductID
197+
INNER JOIN customer AS C on O.C_ID=C.customerID
198+
GROUP BY productname
199+
200+
201+
202+
203+
204+

0 commit comments

Comments
 (0)