You are on page 1of 6

Password: Megamind@123

---------------------------------------------------------------------
postgres=# \l //to view all databases
postgres=# \c SQL-Lab1 //switch to SQL-Lab1 database
SQL-Lab1=# \i D:/Gallery/Downloads/EmployeeDetails.sql

----------------------------------------------------------------------

postgres=# CREATE DATABASE webmonth;


CREATE DATABASE

postgres=# \l //to see list of all database

postgres=# DROP DATABASE webmonth2;


DROP DATABASE

postgres=# \c webmonth
You are now connected to database "webmonth" as user "postgres".

//Create a table inside database


webmonth=# CREATE TABLE users(name text, age smallint, birthday date);
CREATE TABLE

//To see the tables that exits in my database


webmonth=# \d
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | users | table | postgres
(1 row)

//Insert the data in table


webmonth=# INSERT INTO users (name, age, birthday) values ('Prashant', '18', '2002-
12-21');
INSERT 0 1

//Displaying the data


webmonth=# SELECT name, age,birthday from users;
name | age | birthday
----------+-----+------------
Prashant | 18 | 2002-12-21
(1 row)

webmonth=# SELECT * from users;


name | age | birthday
----------+-----+------------
Prashant | 18 | 2002-12-21
(1 row)

//Create a new column


webmonth=# ALTER TABLE users ADD score smallint;
ALTER TABLE
webmonth=# SELECT * from users;
name | age | birthday | score
----------+-----+------------+-------
Prashant | 18 | 2002-12-21 |
(1 row)

//Added data indivisually


webmonth=# UPDATE users set score = 10 WHERE name = 'Prashant';
UPDATE 1
webmonth=# SELECT * from users;
name | age | birthday | score
----------+-----+------------+-------
Prashant | 18 | 2002-12-21 | 10
(1 row)

//Added a new user into TABLE users


webmonth=# INSERT INTO users (name, age, birthday) values ('kishan', '21', '1999-
12-24');
INSERT 0 1
webmonth=# SELECT * from users;
name | age | birthday | score
----------+-----+------------+-------
Prashant | 18 | 2002-12-21 | 10
kishan | 21 | 1999-12-24 |
(2 rows)

//Displaying the data of a indivisual user


webmonth=# SELECT * from users WHERE name = 'Prashant';
name | age | birthday | score
----------+-----+------------+-------
Prashant | 18 | 2002-12-21 | 10
(1 row)

//Added gender to both the users


webmonth=# UPDATE users set gender = 'male' WHERE name = 'Prashant'or name =
'kishan';
UPDATE 2
webmonth=# SELECT * from users;
name | age | birthday | score | gender
----------+-----+------------+-------+--------
Prashant | 18 | 2002-12-21 | 10 | male
kishan | 21 | 1999-12-24 | | male
(2 rows)

//Selecting users whose name is starting from 'P'


webmonth=# SELECT * from users WHERE name like 'P%';
name | age | birthday | score | gender
----------+-----+------------+-------+--------
Prashant | 18 | 2002-12-21 | 10 | male
Pranay | 18 | 2002-12-24 | |
(2 rows)

//Displaying the users data in decending order on basis of score


webmonth=# SELECT * from users order by score desc
webmonth-# ;
name | age | birthday | score | gender
----------+-----+------------+-------+--------
kishan | 21 | 1999-12-24 | | male
Prashant | 18 | 2002-12-21 | 10 | male
Pranay | 18 | 2002-12-24 | 8 |
(3 rows)

//Aggregation operators like:


AVG() – return the average value.
COUNT() – return the number of values.
MAX() – return the maximum value.
MIN() – return the minimum value.
SUM() – return the sum of all or distinct values.

webmonth=# SELECT avg(score) from users;


avg
--------------------
9.0000000000000000
(1 row)

=================================================
webmonth=# SELECT SUM(age) from users;
sum
-----
57
(1 row)
=================OR==============================
webmonth=# SELECT SUM(age) as TOTAL from users;
total
-------
57
(1 row)
=================================================

//Creating a new login TABLE


========================================================================
CREATE TABLE login(ID serial not NULL primary key, name text,email_id varchar(50)
unique not NULL, secret varchar(100) not NULL);

webmonth=# SELECT * from login;


id | secret | name
----+--------+------
(0 rows)

webmonth=# INSERT INTO login(secret,name) values ('abc','Prashant');


INSERT 0 1
webmonth=# INSERT INTO login(secret,name) values ('def','Jack');
INSERT 0 1
webmonth=# SELECT * from login;
id | secret | name
----+--------+----------
1 | abc | Prashant
2 | def | Jack
(2 rows)

webmonth=# INSERT INTO login(secret,name) values ('gfg','Devil');


INSERT 0 1
webmonth=# SELECT * from login;
id | secret | name
----+--------+----------
1 | abc | Prashant
2 | def | Jack
3 | gfg | Devil
(3 rows)

webmonth=# DELETE FROM login WHERE name = 'Devil';


DELETE 1
webmonth=# SELECT * from login;
id | secret | name
----+--------+----------
1 | abc | Prashant
2 | def | Jack
(2 rows)

webmonth=# INSERT INTO login(secret,name) values ('gfg','Devil');


INSERT 0 1
webmonth=# INSERT INTO login(secret,name) values ('qwerty','Angel');
INSERT 0 1
webmonth=# SELECT * from login;
id | secret | name
----+--------+----------
1 | abc | Prashant
2 | def | Jack
4 | gfg | Devil
5 | qwerty | Angel
(4 rows)
=====================================================================

You might also like