Sitemap
Shive& Singh

Shive& Singh

Open for Computer Science Opportunities :)

Press enter or click to view image in full size

SQL

3 min readMar 3, 2024

SQL (structured query language)
It’s a standardized language specifically designed to manage data stored in relational databases.

let’s understand some frequently used and well-known queries of SQL

Let’s imagine we have a list of friends…

1). Let’s create a new table of my friends with some of their details.

query:

create table friends(
name varchar(20), --this stores text data with a character length of 20.
phone int, --this store's whole number for phone numbers.
location varchar(25), -- this stores text data with a character length of 25.
birthday date, --this stores date in 'YYYY-MM-DD' format.
quality varchar(15) --this stores text with a max character length of 15.
);

2). Fill out the details in the newly created table or
insert the values:
query:

insert into friends values('samarth',111,'noida','2001–11–11','cute'),
('shivendra',112,'delhi','2001–09–17','curious'),
('ayushi',113,'banglore','2002–09–05','smart'),
('apporv', 118, 'bareily', '1999–06–03', 'handsome'),
('anarghya',114,'banglore','2001–10–09','adorable'),
('kajal',115,'lucknow','2002–06–08','charismatic'),
('sarthak',116, 'ayodhya', '1998–01–09','golden-heart'),
('prashant', 117, 'gorakhpur', '1990–08–07','cautious');

3). now let’s see if all friends got listed in the table friends or
select all from table

query:

select * from friends;

outputs:

 | name | phone | location | birthday | quality |
__________________________________________________
|samarth | 111 | noida |2001–11–11 |cute |
|shivendra | 112 | delhi |2001–09–17 |curious |
|ayushi | 113 | banglore | 2002–09–05 |smart |
|apporv | 118 | bareily |1999–06–03 |handsome |
|anarghya | 114 | banglore |2001–10–09 |adorable |
|kajal | 115 | lucknow |2002–06–08 |charismatic |
|sarthak | 116 | ayodhya |1998–01–09 |golden-heart|
|prashant | 117 | gorakhpur | 1990–08–07 |cautious |

4).wait! I remember Sarthak is ‘annoying’, so let’s change its quality to annoying which he is most of the time, or
update the quality from the older one to a new one

query:

update friends set quality = 'annoying' where name = 'sarthak';

5). Oh! I just forget to add the email of my friends or
add a new column named email to the table

query:

alter table friends add email varchar(20);

6). No. there is no need to add an email column to the table or

drop a column named email from table
query:

alter table friends drop email;

7) Now, comically shivendra doesn’t like his name, so let’s change the name into something much cooler or
update the name to a new one

query:

update friends set name ='Aadvik' where name = 'shivendra';

8) One second ayushi is not my friend, she is my sister, so let’s remove her from this table or
delete the data of Ayushi from the table

Get Shive& Singh’s stories in your inbox

Join Medium for free to get updates from this writer.

query:

delete from friends where name = 'ayushi';

9). OK let’s take a quick look at the table or
select all from table

query:

select * from friends;

output:

 | name | phone | location | birthday | quality |
____________________________________________________________________________
| samarth | 111 | noida |2001–11–11 | cute |
| Aadvik | 112 | delhi |2001–09–17 | curious |
| apporv | 118 | bareily | 1999–06–03 | handsome |
| anarghya | 114 | banglore | 2001–10–09 | adorable |
| kajal | 115 | lucknow | 2002–06–08 | charismatic |
| sarthak | 116 | ayodhya | 1998–01–09 | annyoying |
| prashant | 117 | gorakhpur | 1990–08–07 | cautious |

NOTE: notice the changes from the above commands.

10). check the birthdate of all friends or
select all names and their birthdate from table

query:

select name, birthday from friends;

output:

 | name | birthday |
___________________________________
| samarth | 2001–11–11 |
| Aadvik | 2001–09–17 |
| apporv | 1999–06–03 |
| anarghya | 2001–10–09 |
| kajal | 2002–06–08 |
| sarthak | 1998–01–09 |
| prashant | 1990–08–07 |

11) Just let me check how many friends I have or
count the number of friends or number of rows in table

query:

select count(name) from friends;

output:

 |count(name)|
____________
| 7 |

12). you know I have some millennial friends too let's see those only or
select all whose birthdates are before year 1999

query:

select names as millennials where birthday < '1999–01–01';

output:

 | millennials |
_______________
| sarthak |
| prashant |

NOTE: here we can change the heading of the column name using clause as.

these are very few but basic and known query commands to get confident to go further in SQL

if you want to learn further and in a better and more fun way
check :

SQL_Tutorial

--

--

Shive& Singh
Shive& Singh

Written by Shive& Singh

Open for Computer Science Opportunities :)