Querying tables
Venkata Sumanth Siddareddy
Aspiring software developer || PERN Stack || Java || Agile Methodology || AI-ML
First Let us see what is mean by keyword, expression, clause, operator in SQL ?
Keywords
A keyword is a reserved word in SQL that has a predefined meaning or purpose. Keywords are the building blocks of clauses and SQL statements.
Expression
An expression is any valid combination of values, operators, functions, and keywords that evaluates to a single value or result. Expressions can be used in various parts of SQL statements, such as clauses, to specify conditions or calculations.
clause
A clause is a distinct component of a SQL statement that performs a specific function, typically consisting of one or more keywords and associated expressions. Clauses work together to define what data is retrieved, manipulated, or stored.
Operator
An operator is a symbol or keyword that performs an operation on one or more values called as operands, and returns a result. Operators are used in expressions to perform comparisons, arithmetic, logical operations, and some other.
Now let us learn some of the clauses and operators to construct queries.
Clauses in PostgreSQL
SELECT
The SELECT clause is used to retrieve data from one or more tables.
FROM
The FROM clause indicates the table or tables from which data is retrieved or deleted.
ORDER BY
The ORDER BY clause sorts the result set based on one or more columns, based on expressions like LENGTH() of a string etc.,
LENGTH() function accepts CHAR, VARCHAR, TEXT datatypes.
WHERE
The WHERE clause filters records based on specified conditions. It applies to individual rows only.
HAVING
The HAVING clause filters results after grouping and aggregation. It applies to groups as a whole.
WHAT is Aggregation? An aggregate is a collection of people who happen to be at the same place at the same time but who have no other connection to one another
GROUP BY
The GROUP BY clause organizes rows into groups based on one or more column values.
领英推荐
LIMIT
The LIMIT clause restricts the number of rows returned in a query.
OFFSET
Used to skip specified number of rows at the beginning of a query result.
FETCH
The FETCH clause retrieves a specific subset of rows from a result set.
Syntax
OFFSET start { ROW | ROWS } FETCH { FIRST | NEXT } [ row_count ] { ROW | ROWS } ONLY
Operators in PostgreSQL
AND
Combines two or more conditions, returning true if all conditions are true.
OR
Combines two or more conditions, returning true if at least one condition is true.
IN
Checks whether a value matches any value in a specified list or subquery. Some of the key points about IN operator are :
LIKE
Performs pattern matching for strings using wildcards. There are 2 special wildcard characters ( % , _ )for the purpose of pattern matching.
BETWEEN
Operator checks if a value falls within a specified range.
NOTE : we use DVD rental database to perform SQL queries using clauses and operators. Set up DVD rental database
If you understand the topics till here you are ??GREAT, Try to implement these queries for hands on experience. Check out this page to find Number of keywords in PostgreSQL
Your Turn! mention any other commonly using operators, keywords, expressions or suggestions and insights in comments below.
Student at Parala Maharaja Engineering College, Berhampur
1 个月We have an opportunity for you regarding your preference of role for our start-up. Skills & Expertise: 1. Backend : Node.js,express 2. Frontend: Javascript, React 3. Database : PostgreSQL/ mongodb Wanloft.com/roles