PostgreSQL-Database/C2/Filtering-and-Sorting-Data/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of the Script: Filtering and Sorting Data

Author: EduPyramids

Keywords: PostgreSQL, pgAdmin 4, WHERE, ORDER BY, data filtering, data sorting, ascending order, descending order, EduPyramids, video tutorial.

Visual Cue Narration
Slide 1

Title Slide

Welcome to this Spoken Tutorial on Filtering and Sorting Data in Postgresql.
Slide 2

Learning Objectives

In this tutorial, we will learn to use,
  • WHERE clause and
  • ORDER BY clause.
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

https://EduPyramids.org

To follow this tutorial,
  • Learners should have PostgreSQL and p g Admin 4.
  • For the prerequisite PostgreSQL tutorials, please visit this website.
Slide 5

Code Files

The following code file is required to practice this tutorial:

  • wo-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.
Slide 6

WHERE Clause

The WHERE clause is used to apply a condition to filter records.

It is used to extract only those records that satisfy a specified condition.

Slide 7

Syntax of Where clause

SELECT column1, column2

FROM table_name

WHERE condition;

The basic syntax of the WHERE clause is shown here.
Point to the interface. I have opened the p g Admin 4 interface and connected to the localhost server.
Show the code file while recording. Let us insert a few records of the students.

Please copy and paste this code from the code file in the query window.

Click the Execute query icon on the tool bar. Let us click the Execute query icon on the toolbar to insert the rows.
SELECT * FROM students

WHERE studentname = 'Ram';

Click the Execute query icon.

Point to the output.

Now I will type this query.

In this query SELECT keyword is used to retrieve data.

WHERE keyword filters the rows and shows all the details of the student named Ram.

Let us execute the query to see the output.

SELECT * FROM students

WHERE studentid = 'S102';

Press the Execute query icon.

Let us type another query using a WHERE clause.

I will execute the query to see the details of the student with I D S 1 0 2.

Next, let us learn about the ORDER BY clause.
Slide 8

ORDER BY Clause

The ORDER BY clause sorts the records in ascending or descending order.
Slide 9

Syntax of ORDER BY Clause

SELECT column1, column2, ...

FROM table_name

ORDER BY column1, column2, ... ASC|DESC;

This is the syntax of the ORDER BY clause.

Here A S C sorts the data in ascending order.

D E S C sorts the data in descending order.

Type: SELECT * FROM students

ORDER BY cgpa;

Highlight ORDER BY

Click the Execute query button.

Let us type this query.

By default ORDER BY clause sorts the records in ascending order.

Click the Execute query icon to execute the query.

Observe that records are sorted by C G P A in ascending order.

Now let us sort the records in descending order.
Type:

SELECT * FROM students

ORDER BY cgpa DESC;

Click the Execute query button.

Let us type this query.

Here the D E S C keyword sorts the records in descending order.

Now let’s execute the query.

The result shows that records are sorted by C G P A in descending order.

Slide 10

SummaryIn this tutorial, we learnt to use

  • WHERE clause and
  • ORDER BY clause
With this, we come to the end of this tutorial.

Let us summarise.

Slide 11

Assignment As an Assignment,

  • Filter student records where city is Bhopal
  • Display all student records in ascending and descending orders sorted by city
We encourage you to do this assignment.
Slide 12

Thank you

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

Thank you for joining.

Contributors and Content Editors

ANJALISATDIVE, Ketkinaina