Difference between revisions of "PostgreSQL-Database/C2/Conditional-Operators/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 45: Line 45:
 
'''Pre-requisites '''
 
'''Pre-requisites '''
  
 +
To follow this tutorial,
 +
* Learners should have '''PostgreSQL''' and '''pgAdmin 4 '''
 +
* For the prerequisite '''PostgreSQL''' tutorials, please visit
  
 
[https://edupyramids.org/ https://EduPyramids.org] '''
 
[https://edupyramids.org/ https://EduPyramids.org] '''
 
  
  
Line 66: Line 68:
 
* '''co-commands.txt'''
 
* '''co-commands.txt'''
  
This file is provided in the Code Files link on this tutorial page.
+
This file is provided in the '''Code Files''' link on this tutorial page.
  
  
Line 95: Line 97:
  
 
'''LIKE operator'''
 
'''LIKE operator'''
 +
 +
 +
The '''LIKE''' operator is used to search for a specified pattern in a column.
 +
 +
It is used along with the '''WHERE''' '''clause'''.
 +
 +
 
|| The '''LIKE''' operator is used to search for a specified pattern in a column.  
 
|| The '''LIKE''' operator is used to search for a specified pattern in a column.  
  
Line 112: Line 121:
 
'''FROM table_name'''
 
'''FROM table_name'''
  
'''WHERE columnN LIKE pattern; '''
+
'''WHERE column LIKE pattern; '''
  
  
Line 152: Line 161:
  
  
The''' WHERE clause''' filters rows and displays the student names starting with '''R'''.  
+
The ''' WHERE clause''' filters rows and displays the student names starting with '''R'''.  
  
  
The''' percent''' sign allows any number of characters after''' R'''.
+
The ''' percent''' sign allows any number of characters after '''R'''.
  
  
Line 176: Line 185:
 
Type the following query.
 
Type the following query.
 
|-  
 
|-  
|| '''Highlight LIKE '%a''''
+
|| '''Highlight LIKE '%a' '''
 
|| This query retrieves '''studentname''' and''' city'''.  
 
|| This query retrieves '''studentname''' and''' city'''.  
  
The pattern ''''%a' '''means any number of characters followed by the letter '''a'''.
+
The pattern within single quotes '''percentage a''' means any number of characters followed by the letter '''a'''.
 
|-  
 
|-  
 
|| '''Click the Execute query icon.'''
 
|| '''Click the Execute query icon.'''
Line 185: Line 194:
  
  
The output shows student from the''' city''' ending with '''a'''.
+
The output shows student from the ''' city''' ending with '''a'''.
 
|-  
 
|-  
 
|| '''Slide 8'''
 
|| '''Slide 8'''
Line 194: Line 203:
  
  
It works with numbers, dates, and text values. The range values are inclusive.
+
It works with numbers, dates, and text values.  
 +
 
 +
The range values are inclusive.
 
|-  
 
|-  
 
|| '''Slide 9'''
 
|| '''Slide 9'''
Line 202: Line 213:
  
  
'''SELECT column name(s)'''
+
'''SELECT column_name(s)'''
  
'''FROM table name'''
+
'''FROM table_name'''
  
'''WHERE column name BETWEEN value1 AND value2;'''
+
'''WHERE column_name BETWEEN value1 AND value2;'''
 
|| This is the '''syntax''' of '''BETWEEN''' operator.
 
|| This is the '''syntax''' of '''BETWEEN''' operator.
 
|-  
 
|-  
Line 216: Line 227:
  
 
Type the following query.
 
Type the following query.
 
 
  
 
|-  
 
|-  
Line 232: Line 241:
  
  
The output shows students whose''' CGPA''' is within the range of 8 and 9.
+
The output shows students whose ''' CGPA''' is within the range of 8 and 9.
 
|-  
 
|-  
 
|| '''Type the command''':  
 
|| '''Type the command''':  
Line 244: Line 253:
  
 
Type the following query.
 
Type the following query.
 
 
  
  
Line 262: Line 269:
  
 
'''NOT BETWEEN Operator'''
 
'''NOT BETWEEN Operator'''
|| The''' NOT BETWEEN''' operator is used to filter values that fall outside a specified range.
+
|| The ''' NOT BETWEEN''' operator is used to filter values that fall outside a specified range.
 
|-  
 
|-  
 
|| '''Slide 11'''
 
|| '''Slide 11'''
Line 270: Line 277:
  
  
'''SELECT column name(s)'''
+
'''SELECT column_name(s)'''
  
'''FROM table name'''
+
'''FROM table_name'''
  
'''WHERE column name NOT BETWEEN'''
+
'''WHERE column_name NOT BETWEEN'''
  
 
'''value1 AND value2;'''
 
'''value1 AND value2;'''
|| This is the syntax of the '''NOT BETWEEN operator.'''
+
|| This is the syntax of the '''NOT BETWEEN operator'''.
 
|-  
 
|-  
 
|| '''Type:'''
 
|| '''Type:'''
Line 287: Line 294:
  
 
'''WHERE cgpa NOT BETWEEN 8 AND 9;'''
 
'''WHERE cgpa NOT BETWEEN 8 AND 9;'''
|| Let us retrieve students whose '''CGPA''' is not between 8 and 9.Type this query.
+
|| Let us retrieve students whose '''CGPA''' is not between 8 and 9.
 +
 
 +
Type this query.
 
|-  
 
|-  
 
|| '''Highlight NOT BETWEEN 8 AND 9'''
 
|| '''Highlight NOT BETWEEN 8 AND 9'''
|| The '''NOT''' '''BETWEEN''' clause returns rows where '''cgpa''' is less than 8 or greater than 9.
+
|| The '''NOT BETWEEN''' clause returns rows where '''cgpa''' is less than 8 or greater than 9.
 
|-  
 
|-  
 
|| Click on the '''execute''' '''query''' icon
 
|| Click on the '''execute''' '''query''' icon
Line 301: Line 310:
  
 
'''IN Operator'''
 
'''IN Operator'''
 +
 +
The '''IN''' operator is used to specify multiple values in a '''WHERE clause'''.
 +
 +
 +
It checks whether a column matches any value in a given list.
 
|| The '''IN''' operator is used to specify multiple values in a '''WHERE clause'''.
 
|| The '''IN''' operator is used to specify multiple values in a '''WHERE clause'''.
  
Line 311: Line 325:
  
  
'''SELECT column name(s)'''
+
'''SELECT column_name(s)'''
  
'''FROM table name'''
+
'''FROM table_name'''
  
'''WHERE column name IN (value1,'''
+
'''WHERE column_name IN (value1,'''
  
 
'''value2, ...);'''
 
'''value2, ...);'''
Line 326: Line 340:
  
 
'''WHERE city IN ('Mumbai', 'Pune', 'Delhi');'''
 
'''WHERE city IN ('Mumbai', 'Pune', 'Delhi');'''
 
  
  
Line 355: Line 368:
  
  
'''SELECT column name(s)'''
+
'''SELECT column_name(s)'''
  
'''FROM table name'''
+
'''FROM table_name'''
  
'''WHERE column name NOT IN (value1,'''
+
'''WHERE column_name NOT IN (value1,'''
  
 
'''value2, ...);'''
 
'''value2, ...);'''
Line 399: Line 412:
 
'''Syntax of DISTINCT Operator'''
 
'''Syntax of DISTINCT Operator'''
  
'''SELECT DISTINCT column-names FROM table-name WHERE [condition]'''
+
'''SELECT DISTINCT column_names FROM table_name WHERE [condition];'''
|| The '''syntax''' is as follows:
+
  
 +
|| The '''syntax''' is as follows:
  
  
Line 436: Line 449:
 
|| Let us summarise.
 
|| Let us summarise.
 
|-  
 
|-  
|| '''Slide 20 +21+22'''
+
|| '''Slide 20 + 21+ 22'''
  
  

Latest revision as of 13:31, 28 April 2026

Title of the script: Conditional Operators

Authors: Ketki Bhamble EduPyramids

Keywords: PostgreSQL, pgAdmin 4, SQL, WHERE Clause, LIKE, percent sign, underscore, BETWEEN, NOT BETWEEN, IN, NOT IN, DISTINCT, EduPyramids, video tutorial.


Visual Cue Narration
Slide 1


Title Slide

Welcome to this Spoken Tutorial on Conditional Operators in PostgreSQL.
Slide 2


Learning Objectives

In this tutorial, we will learn about,
  • LIKE, BETWEEN, NOT BETWEEN, IN, NOT IN and DISTINCT operators


Slide 3


System Requirements


  • Ubuntu 24.04 LTS
  • PostgreSQL version 18.1, and
  • PgAdmin 4 version 9.11.


To record this tutorial, I am using the following setup.
Slide 4


Pre-requisites

To follow this tutorial,

  • Learners should have PostgreSQL and pgAdmin 4
  • For the prerequisite PostgreSQL tutorials, please visit

https://EduPyramids.org


To follow this tutorial,
  • Learners should have PostgreSQL and pgAdmin 4
  • For the prerequisite PostgreSQL tutorials, please visit this website.


Slide 5


Code Files


The following code file is required to practice this tutorial:

  • co-commands.txt

This file is provided in the Code Files link on this tutorial page.


Please download and extract the file.

The following code file is required to practice this tutorial.


This file is provided in the Code Files link of this tutorial page.


Please download and extract the file.

Let us get started.
Show students-table

Type:

SELECT * FROM students;

We are using the students table.


It contains studentid, studentname, city, gender, date of birth, and cgpa.

Slide 6


LIKE operator


The LIKE operator is used to search for a specified pattern in a column.

It is used along with the WHERE clause.


The LIKE operator is used to search for a specified pattern in a column.

It is used along with the WHERE clause.


Slide 7


Syntax of LIKE operator


SELECT column1, column2, ...

FROM table_name

WHERE column LIKE pattern;


Highlight pattern keyword.

This is the syntax of the LIKE operator.

The pattern may contain percent sign or underscore.


Percent sign represents zero or more characters.

Underscore represents exactly one character.

Type the command:


SELECT * FROM students

WHERE studentname LIKE 'R%';


Highlight:*


Highlight: WHERE


Highlight:‘R%’


Click on the execute query button.


Let us retrieve the records of students whose names start with the letter R.


Type the following query.


The WHERE clause filters rows and displays the student names starting with R.


The percent sign allows any number of characters after R.


Let us execute the query.


The output displays students whose names start with R.

Type the query:


SELECT studentname, city

FROM students

WHERE city LIKE '%a';

Now let’s find students whose city name ends with letter a using the WHERE clause.


Type the following query.

Highlight LIKE '%a' This query retrieves studentname and city.

The pattern within single quotes percentage a means any number of characters followed by the letter a.

Click the Execute query icon. Let us execute the query.


The output shows student from the city ending with a.

Slide 8


BETWEEN Operator

The BETWEEN operator is used to filter values within a specified range.


It works with numbers, dates, and text values.

The range values are inclusive.

Slide 9


Syntax of BETWEEN Operator


SELECT column_name(s)

FROM table_name

WHERE column_name BETWEEN value1 AND value2;

This is the syntax of BETWEEN operator.
Type the command:

SELECT studentname, cgpa FROM students WHERE cgpa BETWEEN 8 AND 9;

Let us now find students whose CGPA is between 8 and 9.


Type the following query.

Highlight BETWEEN 8 AND 9 This query retrieves the studentname and c g p a.


The BETWEEN clause filters rows where CGPA is between 8 and 9.

It includes values greater than or equal to 8 and less than or equal to 9.

Click the Execute query icon. Let us execute the query.


The output shows students whose CGPA is within the range of 8 and 9.

Type the command:

SELECT studentname, dob FROM students WHERE dob BETWEEN '1996-01-01' AND '1996-12-31';


Highlight dob BETWEEN '1996-01-01' AND '1996-12-31'

Now, let us find students born in the year 1996.


Type the following query.


This query retrieves the studentname and date of birth.


The BETWEEN clause filters students born between the specified dates.

Click the Execute query icon. Let us execute the query.

The output displays students born in the year 1996.

Slide 10


NOT BETWEEN Operator

The NOT BETWEEN operator is used to filter values that fall outside a specified range.
Slide 11


Syntax of NOT BETWEEN Operator


SELECT column_name(s)

FROM table_name

WHERE column_name NOT BETWEEN

value1 AND value2;

This is the syntax of the NOT BETWEEN operator.
Type:


SELECT studentname, cgpa

FROM students

WHERE cgpa NOT BETWEEN 8 AND 9;

Let us retrieve students whose CGPA is not between 8 and 9.

Type this query.

Highlight NOT BETWEEN 8 AND 9 The NOT BETWEEN clause returns rows where cgpa is less than 8 or greater than 9.
Click on the execute query icon Let us execute the query.


The output displays students whose CGPA falls outside this range.

Slide 13

IN Operator

The IN operator is used to specify multiple values in a WHERE clause.


It checks whether a column matches any value in a given list.

The IN operator is used to specify multiple values in a WHERE clause.


It checks whether a column matches any value in a given list.

Slide 14

Syntax of IN Operator


SELECT column_name(s)

FROM table_name

WHERE column_name IN (value1,

value2, ...);

This is the syntax of the IN operator.
Type:


SELECT * FROM students

WHERE city IN ('Mumbai', 'Pune', 'Delhi');


Let us retrieve students who belong to Mumbai, Pune, or Delhi.


Type the following query.

Highlight city IN ('Mumbai','Pune','Delhi') In this query, the IN operator matches the city with the specified values.
Click on the execute query icon Click on the Execute query icon to execute the query.


The output displays students from Mumbai, Pune, and Delhi.

Slide 15


NOT IN Operator

The NOT IN operator excludes specified values from the result set.
Slide 16


Syntax of NOT IN Operator


SELECT column_name(s)

FROM table_name

WHERE column_name NOT IN (value1,

value2, ...);

This is the syntax of the NOT IN operator.
Type:

SELECT * FROM students

WHERE city NOT IN ('Mumbai','Pune');

Now let us retrieve students who do not belong to Mumbai or Pune.


Type the following query.

Highlight NOT IN ('Mumbai','Pune') The NOT IN operator filters out the specified cities and returns the remaining records.
Click on the execute query icon Let us execute the query.


The output shows students from other cities except Mumbai and Pune.

Slide 17


DISTINCT Operator


DISTINCT keyword is used in the SELECT statement to list distinct and unique values.

It removes the duplicate rows or column values from the query result.

Slide 18


Syntax of DISTINCT Operator

SELECT DISTINCT column_names FROM table_name WHERE [condition];

The syntax is as follows:


Type the command:

SELECT DISTINCT city FROM students;


Type the query as shown here.

This select statement will retrieve the unique city names from the students table.

Let us execute the query.

Output will eliminate duplicate values from the specified column city.

It only shows distinct city names.

With this, we come to the end of this tutorial.
Slide 19


Summary

In this tutorial, we have learnt about

  • LIKE, BETWEEN, NOT BETWEEN, IN, NOT IN and DISTINCT operators


Let us summarise.
Slide 20 + 21+ 22


Assignment


As an Assignment,

  • Retrieve student records whose names start with letter A using the LIKE operator.
  • Display the studentname and city where city names contain the letter “n”.
  • List students who belong to Chennai, Kolkata, or Bengaluru using the IN operator.
  • Find students whose CGPA is between 7 and 8.5.
  • Display students records who are born after 1997 using the NOT BETWEEN operator.
  • Retrieve the distinct gender values from the students table.
  • Display students whose city is neither Chennai nor Jaipur.


As an Assignment, please do the following.
Slide 23

This Spoken Tutorial is brought to you by EduPyramids Educational Private Limited SINE IIT Bombay.

Thank you

Thank You.

Contributors and Content Editors

Madhurig