0% found this document useful (0 votes)
3 views

SQL functionts

The document provides various SQL queries and techniques for data manipulation in PostgreSQL, including salary checks, string functions, aggregate functions, and date/time functions. It demonstrates the use of operators like LIKE and IN, as well as conditional functions such as CASE and IF. Additionally, it covers methods for extracting and formatting data, along with counting unique departments and total products.

Uploaded by

Mateen Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL functionts

The document provides various SQL queries and techniques for data manipulation in PostgreSQL, including salary checks, string functions, aggregate functions, and date/time functions. It demonstrates the use of operators like LIKE and IN, as well as conditional functions such as CASE and IF. Additionally, it covers methods for extracting and formatting data, along with counting unique departments and total products.

Uploaded by

Mateen Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL QUERY

"SQL for Smarties" by Joe Celko


"PostgreSQL: Up and Running" by Regina Obe and Leo Hsu

-- salary check

SELECT first_name, last_name, salary


FROM employee2
WHERE salary BETWEEN 40000 AND 60000;

-- USE LIKE OPERATOR

SELECT first_name FROM employee2


WHERE first_name LIKE '%a';

-- USE of IN Operator

SELECT first_name, last_name, department


FROM employee2
WHERE department IN ('Finance','Marketing');

-- Unique 5 department

SELECT first_name, last_name, department


FROM employee2
ORDER BY salary DESC
LIMIT 5;

SELECT COUNT (DISTINCT department) AS DEP_UNIQUE_COUNT


FROM employee2;

– Aggregate Functions
Max , Min

SELECT SUM(quantity) AS total_quantity


FROM table_name

–Total number of products

SELECT COUNT(*) AS total_products


FROM —--- ;
Average price

SELECT AVG(price) As average_price


FROM —---;

String Functions

LENGTH —-- SELECT department, LENGTH(department) AS count_name


FROM employee2;

-- To Join to column together


SELECT CONCAT(first_name,' ', last_name) AS full_name
FROM employee2;

To Extract character

SELECT SUBSTRING (first_name,1,5) AS short_name


FROM —----- Table name;

— Removing Trailing and leading spaces

SELECT TRIM

-- Date / Time functions

SELECT CURRENT_DATE AS today_date;

SELECT joining_date,CURRENT_DATE, (CURRENT_DATE- joining_date) AS date_difference


FROM employee2;

EXTRACT

SELECT department, EXTRACT (YEAR FROM joining_date) AS year_date


FROM employee2;

AGE

SELECT department,
AGE (CURRENT_DATE ,joining_date) AS age_calulation
FROM employee2;
– To Char
SELECT department,
TO_CHAR(joining_date,'DD-MON-YYYY') AS format_date
FROM employee2;

SELECT department, joining_date,


DATE_PART('dow',joining_date) AS format_date
FROM employee2;

--Date Trunc

SELECT department, joining_date,


DATE_TRUNC('week',joining_date) AS week_date,
DATE_PART('MONTH',joining_date) AS month_date
FROM employee2;

—-------------------
SELECT department, joining_date,
joining_date + INTERVAL '8 MONTHS' AS NEW_date
FROM employee2;

Conditional Function

SELECT
column_name,
CASE
WHEN condition1 THEN 'result1'
WHEN condition2 THEN 'result2'
ELSE 'default_result'
END AS alias_name
FROM table_name;

SELECT
IF(condition, 'true_value', 'false_value') AS alias_name
FROM table_name;

SELECT
IFNULL(column_name, 'default_value') AS alias_name
FROM table_name;
SELECT
NULLIF(expression1, expression2) AS alias_name
FROM table_name;

You might also like