❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

SQL – Postgres – Few Advance Topics

By: Sugirtha
29 December 2024 at 09:31

The order of execution in a SQL query:

FROM and/or JOIN
WHERE
GROUP BY
HAVING
SELECT
DISTINCT
ORDER BY
LIMIT nad/or OFFSET

Command Types:

References : Aysha Beevi

CAST()

CAST is used to typecast or we can use ::target data type.

SELECT β€˜The current date is: β€˜ || CURRENT_DATE::TEXT;
SELECT β€˜2024-12-21’::DATE::TEXT;
SELECT CAST(β€˜2024-12-21’ AS DATE);

|| –> Concatenation operator

DATE functions:

SELECT CURRENT_DATE; β€” Output: 2024-12-21
SELECT CURRENT_TIME; β€” Output: 09:15:34.123456+05:30
SELECT NOW(); β€” Output: 2024-12-21 09:15:34.123456+05:30
SELECT AGE(β€˜2020-01-01’, β€˜2010-01-01’); β€” Output: 10 years 0 mons 0 days
SELECT AGE(β€˜1990-05-15’); β€” Output: 34 years 7 mons 6 days (calculated from NOW())
SELECT EXTRACT(YEAR FROM NOW()); β€” Output: 2024
SELECT EXTRACT(MONTH FROM CURRENT_DATE); β€” Output: 12
SELECT EXTRACT(DAY FROM TIMESTAMP β€˜2024-12-25 10:15:00’); β€” Output: 25

The DATE_TRUNC() function truncates a date or timestamp to the specified precision. This means it β€œresets” smaller parts of the date/time to their starting values.
SELECT DATE_TRUNC(β€˜month’, TIMESTAMP β€˜2024-12-21 10:45:30’);
β€” Output: 2024-12-01 00:00:00 –> The β€˜month’ precision resets the day to the 1st, and the time to 00:00:00.
SELECT DATE_TRUNC(β€˜year’, TIMESTAMP β€˜2024-12-21 10:45:30’);
β€” Output: 2024-01-01 00:00:00
SELECT DATE_TRUNC(β€˜day’, TIMESTAMP β€˜2024-12-21 10:45:30’);
β€” Output: 2024-12-21 00:00:00

SELECT NOW() + INTERVAL β€˜1 year’;
β€” Output: Current timestamp + 1 year
SELECT CURRENT_DATE – INTERVAL ’30 days’;
β€” Output: Today’s date – 30 days
SELECT NOW() + INTERVAL β€˜2 hours’;
β€” Output: Current timestamp + 2 hours
SELECT NOW() + INTERVAL β€˜1 year’ + INTERVAL β€˜3 months’ – INTERVAL ’15 days’;

Window Functions

This is the function that will operate over the specified window. Common window functions include ROW_NUMBER(), RANK(), SUM(), AVG(), etc

.PARTITION BY: (Optional) Divides the result set into partitions to which the window function is applied. Each partition is processed separately.ORDER BY: (Optional) Orders the rows in each partition before the window function is applied.

window_function() OVER (--RANK() or SUM() etc. can come in window_function
    PARTITION BY column_name(s)
    ORDER BY column_name(s)
 );

SELECT 
    department_id,
    employee_id,
    salary,
    SUM(salary) OVER (PARTITION BY department_id ORDER BY salary) AS running_total
FROM employees;

CURSOR:

DO $$
DECLARE
emp_name VARCHAR;
emp_salary DECIMAL;
emp_cursor CURSOR FOR SELECT name, salary FROM employees;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_name, emp_salary;
EXIT WHEN NOT FOUND; β€” Exit the loop when no rows are left
RAISE NOTICE β€˜Employee: %, Salary: %’, emp_name, emp_salary;
END LOOP;
CLOSE emp_cursor;

Basic Data Types in PostgreSQL

TEXT, VARCHAR, CHAR: Working with strings.
INTEGER, BIGINT, NUMERIC: Handling numbers.
DATE, TIMESTAMP: Date and time handling.

OVER CLAUSE

In PostgreSQL, the OVER() clause is used in window functions to define a window of rows over which a function operates. Just create a serial number (Row_number) from 1 (Rows are already ordered by salary desc)
SELECT name, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num
FROM employees
WHERE row_num <= 5;

RANK()

Parition the table records based on the dept id, then inside each partition order by salary desc with rank 1,2,3… – In RANK() if same salary then RANK repeats.

SELECT department_id, name, salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank
FROM employees
Output:
department_id name salary rank
101 Charlie 70,000 1
101 Alice 50,000 2
101 Frank 50,000 2
102 Eve 75,000 1
102 Bob 60,000 2
103 David 55,000 1

  • Divides employees into 3 equal salary buckets (quartiles).
    SELECT id, name, salary,
    NTILE(3) OVER (ORDER BY salary DESC) AS quartile
    FROM employees;
    id name salary quartile
    5 Eve 75,000 1
    3 Charlie 70,000 1
    2 Bob 60,000 2
    4 David 55,000 2
    1 Alice 50,000 3
    6 Frank 50,000 3
  • Retrieves the first name in each department based on descending salary.
    SELECT department_id, name, salary,
    FIRST_VALUE(name) OVER (PARTITION BY department_id ORDER BY salary DESC) AS top_earner
    FROM employees;
    Output:
    department_id name salary top_earner
    101 Charlie 70,000 Charlie
    101 Alice 50,000 Charlie
    101 Frank 50,000 Charlie
    102 Eve 75,000 Eve
    102 Bob 60,000 Eve
    103 David 55,000 David

First from table will be taken, then WHERE condition will be applied

  • In the WHERE clause directly you cannot call the RANK(), it should be stored in result set, from there only we can call it. So only RANK() will get executed ie Windows CTE (Common Table Expression), that’s why first the CTE will get executed and stored in a temp result set, then SELECT from that result set.
  • Below we gave in the subquery, so it will get executed and then that value is getting used by the outer query.

In each dept top earner name with his name and salary (consider the above table employees)
SELECT department_id, name, salary
FROM (
SELECT department_id, name, salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank
FROM employees
) ranked_employees
WHERE rank = 1;

department_id name salary
101 Charlie 70,000
102 Eve 75,000
103 David 55,000

Resultset – here RankedSalaries is Resultset

WITH RankedSalaries AS (
SELECT salary, RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees
)
SELECT salary
FROM RankedSalaries WHERE rank = 2;

Here, RankedSalaries is a temporary result set or CTE (Common Table Expression)

Reference: Learnt from ChatGPT and Picture from Ms.Aysha

My interview on Malai Talks

12 July 2024 at 03:32

Last week, had a discussion with MalaiKannan on Linux, Linux Users Groups, Kaniyam, Tamil Computing, Ebooks, ML/AI etc. Today he published the talk on his youtube channel β€œMalai Talks” .

See it here –

Shhh. It is a 2 hrs talk. Get prepared to hear long stories.

Sivaprabu, a friend from ilugc, shared a small clip of him seeing the talk on big TV. So happy πŸ™‚

Weekly Notes 27 2024

4 July 2024 at 01:16

Last week was an event full week.

  • Last Thursday, at Heartcomonos,we had a potluck and drumming Jam. Went with friends, Nithya and kids. Good to see around 40 people. Few people came from Ukraine and other countries. Happy to see that heartcomonos is connecting local people, provides a friendly environment for all who miss the back home friends and relatives. The Drumming Jam was mesmerizing. Imagine 40 people are hitting hand drum with same rhythm. It induced the interesting learning drum. But, I remembered all the pending planned activities and a sleeping keyboard. In few months, I have to complete all the planned works and then only can explore the musical keyboard at home.
  • Took few photos and videos of the event. With Darktable, I can mass edit, apply filters to all the photos. But video editing was always a nightmare for me. After exploring OpenShot, ShotCut, found that KDEnlive is an excellent tool for video editing. Though it asks efforts to learn it, the time and efforts are worth. we can all the video magics with it. Edited few event videos with kdenlive.
  • Found good tamil video tutorials – https://www.youtube.com/watch?v=SsQ_RnSTkSg&list=PLhPOYM1ZXzOFPb-tvIGxpykG4jicBnitI

    Thanks to Prasanna for making wonderful tutorial. Thanks for your beautiful cover images for FreeTamilEbooks.com too.

    With good tools and methods, we can really do things quickly. Started to like Kdenlive after Emacs. Feeling like Emacs is for text and Kdenlive is for Videos.

  • Visited BAPS Swami narayana temple in Etobicoke. It has stunning architecture. Kids are seeing many gods for the first time. We discussed about various religions, faiths and rituals. They more they ask questions, the more I say as they are just stories.
  • When Nithya went to the show, Myself and kids watched his show β€œAlex in Wonderland” in Amazon Prime. Don’t miss the show if you like Tamil movies and songs. Many songs and singers were spoken in the show. Watched those songs along with the kids. Kids wondered on Karnan Sivaji, Muthu Rajini etc. Demonstrated how ARR started new wave in tamil music, with different songs. We placed a water bottle on top of the woofer. As the songs go, the bottle started to move and fell down. Discussed few basic science behind it.
  • Last Monday, celebrated Canada day with a day out and fireworks at night.
  • Yesterday, Spoke at a podcast about kaniyam, LUGs, Saama and other activities. It will be released after editing soon.
  • Wrote code to find the unused kubernetes deployments by the loadbalancer’s request count and mapping with the Route53 entries. Programming is the best drug to be immersed, next to reading books.
  • Playing with airflow on kubernetes for its performance fine tuning.

Interesting weeks are ahead. Will share more news as they happen. Do you write blogs? Reply here with your blog URL. I will follow them.

I feel like reading blogs are more peaceful than any other social media.

❌
❌