DEV Community

R.Shobika CSE
R.Shobika CSE

Posted on • Edited on

PostgreSQL day-2

CREATE TABLE:

CREATE table student(
stdroll int,
stdname varchar(20),
department varchar(20),
year int);
Enter fullscreen mode Exit fullscreen mode

INSERT :

INSERT INTO students values(01,'shobi','CSE',4),(02,'Nandhini','CSE',4),(03,'Sandra','ECE',2),(05,'Hindhu','EEE',1);
Enter fullscreen mode Exit fullscreen mode

output:

SELECT:

SELECT * from students;
Enter fullscreen mode Exit fullscreen mode

output:

1 . SPECIFICATION SELECTION IN THE TABLE :

SELECT stdname,stddept from students;
Enter fullscreen mode Exit fullscreen mode

output:

ALTER THE TABLE:

ALTER table students rename to std;
Enter fullscreen mode Exit fullscreen mode

output:

SELECT DISTINCT

The SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values and sometimes you only want to list the different (distinct) values.

SELECT distinct stddept from std;
Enter fullscreen mode Exit fullscreen mode

output:

IF doesnot use the distinct means it will return only selected column values only

WHERE CLAUSE:

  • The WHERE clause is used to filter records.
SELECT * from std WHERE stddept='CSE';
Enter fullscreen mode Exit fullscreen mode

output:

WHERE and:

  • WHERE and is same like the other programming language like if both condition true means its execute , if anyone condition false means it execute 0 rows.
SELECT * from student WHERE stddept='ECE' and year=4;
Enter fullscreen mode Exit fullscreen mode

output:


SELECT * from student WHERE stddept='CSE' and year=4;
Enter fullscreen mode Exit fullscreen mode

output:

WHERE or:

WHERE or is also same like other programming language if anyone condition true means it will execute otherwise its execute 0 rows.

SELECT * from student WHERE stdname='shobi' or stddept='ECE';
Enter fullscreen mode Exit fullscreen mode

ouput:


SELECT * from student WHERE stdname='divya' or stddept='AGRI';
Enter fullscreen mode Exit fullscreen mode

output:

AS:

SQL aliases are used to give a table, or a column in a table, a temporary name.

SELECT stddept as department from student;

Enter fullscreen mode Exit fullscreen mode

output:

*WHERE NOT : *

SELECT * from student WHERE stddept <> 'ECE';

or 

SELECT * from student WHERE != stddept='ECE';

or

SELECT * from student WHERE not stddept='ECE';
Enter fullscreen mode Exit fullscreen mode

output:

> GREATER THAN:

SELECT * from student WHERE year>2;

Enter fullscreen mode Exit fullscreen mode

output:

< LESS THAN:

SELECT * from student WHERE year>4;

Enter fullscreen mode Exit fullscreen mode

output:

BETWEEN:

The BETWEEN operator is used to specify the within the range of the given value.

SELECT * from student WHERE stdname BETWEEN 'hindhu' AND ' shobi';
Enter fullscreen mode Exit fullscreen mode

output:

IN :

The IN operator is used to specify the list of possible value .

SELECT * from student WHERE stddept in ('CSE','ECE');
Enter fullscreen mode Exit fullscreen mode

output:

ORDER BY:

Order by is used to sort the values in the database usually order by give the values in ascending order. we can also change it in descending order.

SELECT * from student ORDERBY stdname;

Enter fullscreen mode Exit fullscreen mode

output:

If you want the student name in descending order means you have to give the desc ;

SELECT * from student ORDE BY stdname desc;
Enter fullscreen mode Exit fullscreen mode

output:

LIMIT:

Limit is used to get the limited amount of data like if we want first 5 data means we can give limit 5 then it display the first 5 data.

SELECT stdrollno, stdname from student limit 3;
Enter fullscreen mode Exit fullscreen mode

output:

In Limit we can use the order by keyword

SELECT * from student ORDER BY stddept limit 3;
Enter fullscreen mode Exit fullscreen mode

output:

LIKE :

Like operator is used to get the values in database based on starting letter, ending letter and also inbetween letter

  • if you want starting letter means 's%';

  • if you want ending letter means '%s';

  • if you want inbetween letter means '%a%';

SELECT stdname from student WHERE stdname LIKE '%i';
Enter fullscreen mode Exit fullscreen mode

output:


SELECT stdname from student WHERE stdname LIKE 'h%';
Enter fullscreen mode Exit fullscreen mode

output:


SELECT stdname from stduent where stdname like '%a%';
Enter fullscreen mode Exit fullscreen mode

output:

Top comments (0)