Difference between revisions of "PostgreSQL-Database/C2/Conditional-Operators/English"
| (2 intermediate revisions 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. |
| + | |||
| + | |||
| + | Please download and extract the file. | ||
|| The following '''code file''' is required to practice this tutorial. | || The following '''code file''' is required to practice this tutorial. | ||
| Line 92: | 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 109: | Line 121: | ||
'''FROM table_name''' | '''FROM table_name''' | ||
| − | '''WHERE | + | '''WHERE column LIKE pattern; ''' |
| Line 149: | 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 173: | 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 ''' | + | 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 182: | 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 191: | 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 199: | Line 213: | ||
| − | '''SELECT | + | '''SELECT column_name(s)''' |
| − | '''FROM | + | '''FROM table_name''' |
| − | '''WHERE | + | '''WHERE column_name BETWEEN value1 AND value2;''' |
|| This is the '''syntax''' of '''BETWEEN''' operator. | || This is the '''syntax''' of '''BETWEEN''' operator. | ||
|- | |- | ||
| Line 213: | Line 227: | ||
Type the following query. | Type the following query. | ||
| − | |||
| − | |||
|- | |- | ||
| Line 229: | 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 241: | Line 253: | ||
Type the following query. | Type the following query. | ||
| − | |||
| − | |||
| Line 259: | 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 267: | Line 277: | ||
| − | '''SELECT | + | '''SELECT column_name(s)''' |
| − | '''FROM | + | '''FROM table_name''' |
| − | '''WHERE | + | '''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 284: | 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 | + | || 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 298: | 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 308: | Line 325: | ||
| − | '''SELECT | + | '''SELECT column_name(s)''' |
| − | '''FROM | + | '''FROM table_name''' |
| − | '''WHERE | + | '''WHERE column_name IN (value1,''' |
'''value2, ...);''' | '''value2, ...);''' | ||
| Line 323: | Line 340: | ||
'''WHERE city IN ('Mumbai', 'Pune', 'Delhi');''' | '''WHERE city IN ('Mumbai', 'Pune', 'Delhi');''' | ||
| − | |||
| Line 352: | Line 368: | ||
| − | '''SELECT | + | '''SELECT column_name(s)''' |
| − | '''FROM | + | '''FROM table_name''' |
| − | '''WHERE | + | '''WHERE column_name NOT IN (value1,''' |
'''value2, ...);''' | '''value2, ...);''' | ||
| Line 396: | Line 412: | ||
'''Syntax of DISTINCT Operator''' | '''Syntax of DISTINCT Operator''' | ||
| − | '''SELECT DISTINCT | + | '''SELECT DISTINCT column_names FROM table_name WHERE [condition];''' |
| − | + | ||
| + | || The '''syntax''' is as follows: | ||
| Line 433: | 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
|
Welcome to this Spoken Tutorial on Conditional Operators in PostgreSQL. |
| Slide 2
|
In this tutorial, we will learn about,
|
| Slide 3
|
To record this tutorial, I am using the following setup. |
| Slide 4
To follow this tutorial,
|
To follow this tutorial,
|
| Slide 5
This file is provided in the Code Files link on this tutorial page.
|
The following code file is required to practice this tutorial.
|
| Let us get started. | |
| Show students-table
Type: SELECT * FROM students; |
We are using the students table.
|
| Slide 6
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
FROM table_name WHERE column LIKE pattern;
|
This is the syntax of the LIKE operator.
The pattern may contain percent sign or underscore.
Underscore represents exactly one character. |
| Type the command:
WHERE studentname LIKE 'R%';
|
Let us retrieve the records of students whose names start with the letter R.
|
| Type the query:
FROM students WHERE city LIKE '%a'; |
Now let’s find students whose city name ends with letter a using the WHERE clause.
|
| 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.
|
| Slide 8
|
The BETWEEN operator is used to filter values within a specified range.
The range values are inclusive. |
| Slide 9
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.
|
| Highlight BETWEEN 8 AND 9 | This query retrieves the studentname and c g p a.
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.
|
| Type the command:
SELECT studentname, dob FROM students WHERE dob BETWEEN '1996-01-01' AND '1996-12-31';
|
Now, let us find students born in the year 1996.
|
| Click the Execute query icon. | Let us execute the query.
The output displays students born in the year 1996. |
| Slide 10
|
The NOT BETWEEN operator is used to filter values that fall outside a specified range. |
| Slide 11
FROM table_name WHERE column_name NOT BETWEEN value1 AND value2; |
This is the syntax of the NOT BETWEEN operator. |
| Type:
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.
|
| Slide 13
IN Operator 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.
|
| Slide 14
Syntax of IN Operator
FROM table_name WHERE column_name IN (value1, value2, ...); |
This is the syntax of the IN operator. |
| Type:
WHERE city IN ('Mumbai', 'Pune', 'Delhi');
|
Let us retrieve students who belong to Mumbai, Pune, or Delhi.
|
| 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.
|
| Slide 15
|
The NOT IN operator excludes specified values from the result set. |
| Slide 16
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.
|
| 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.
|
| Slide 17
|
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
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
In this tutorial, we have learnt about
|
Let us summarise. |
| Slide 20 + 21+ 22
|
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. |