Skip to content

Instantly share code, notes, and snippets.

@theachyutkadam
Last active June 21, 2022 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theachyutkadam/f4cd1d39ad887cc216ac25bcd8ed92ad to your computer and use it in GitHub Desktop.
Save theachyutkadam/f4cd1d39ad887cc216ac25bcd8ed92ad to your computer and use it in GitHub Desktop.
\l - for show all databases
\c database_name- for change/select database.
\d - list of database tables
select * from customers;
#show customers table record.
SELECT first_name FROM customer;
#show particular table row;
SELECT first_name FROM customer;
#concatination of columns
#addition of numbers;
SELECT 5 * 3;
# add alies as a meaning full column name
# as keyword is optional.
select first_name || ' ' || last_name as full_name from students;
select first_name || ' ' || last_name as "full_name" from students;
select first_name || ' ' || last_name "full_name" from students;
Order by
select first_name || ' ' || last_name as full_name, contact, birth_date from customers order by full_name asc;
# set null value at last of first
select * from classrooms order by division nulls first;
select * from classrooms order by division asc nulls larst;
#DISTINCT/unique record show
select DISTINCT division from classrooms;
#limit/offset- show number of record, offeset-skip last number of record
select first_name, last_name from students order by first_name desc limit 5 offset 1;
# fetch number of rows
select first_name, last_name from students order by first_name desc FETCH first 5 ROW ONLY;
# where queries.
select first_name, last_name from students where first_name = 'first_aa29';
where with and condition
select first_name, last_name, contact, birth_date from customers where first_name = 'Caryl' and last_name = 'Waters';
where with or condition
select first_name, last_name, contact, birth_date from customers where first_name = 'Caryl' or last_name = 'Dooley';
where with In contion
select first_name, last_name from customers where last_name IN ('Dooley','Graham','Waters');
#find record with start with specific letter
1- 'k%' starting wiht letter k
1- '%s' end wiht letter s
select first_name, last_name, contact, birth_date from customers where last_name like '%son';
# where with not equal record - != and <> symbol, both are same.
select first_name, last_name, length(last_name) last_name_length from customers where first_name like 'An%' and last_name != 'Wyman';
#find record using from date to end date
select first_name, last_name, contact, birth_date from customers where birth_date between '1996-10-09' and '1999-10-09' ;
# find record which have present particular id
select * from classrooms where teacher_id in (2, 5);
# find record which have not present particular id
select * from classrooms where teacher_id not in (2, 5);
or
select * from classrooms where teacher_id <> 1;
# sub queries
select id, first_name, last_name, contact, birth_date from customers where id in(select customer_id from transactions where account_id = 64);
#show record where query with in between.
select account_type, balance, status from accounts where balance not between 13019 and 21846 order by balance;
# join quries
SELECT c.id, first_name, last_name, birth_date, contact FROM customers c INNER JOIN transactions t ON t.customer_id = c.id ORDER BY first_name DESC;
SELECT c.id, first_name, last_name, birth_date, contact FROM customers c INNER JOIN transactions t ON t.customer_id = c.id where c.birth_date between '1996-10-09' and '2022-10-09' ORDER BY first_name DESC;
SELECT
c.id,
c.first_name customer_first_name,
c.last_name customer_last_name,
a.balance account_balance,
a.customer_id account_customer_id,
e.salery
FROM
customers c
INNER JOIN employees e
INNER JOIN transactions t
ON t.customer_id = c.id
INNER JOIN accounts a
ON t.account_id = a.id
ORDER BY e.salery;
#group by
- use for fetch the unique record of table.
select medium from classrooms group by medium;
select division, sum(teacher_id) from classrooms group by division;
select division, sum(teacher_id) from classrooms group by division order by sum(teacher_id) desc;
SELECT
first_name || ' ' || last_name full_name,
SUM (intake) intake
FROM
classrooms
INNER JOIN teachers USING (id)
GROUP BY
full_name
ORDER BY intake DESC;
SELECT
division,
COUNT (teacher_id)
FROM
classrooms
GROUP BY
division;
SELECT "classrooms".* FROM "classrooms" WHERE "classrooms"."division" != 'A' AND "classrooms"."intake" = 78 LIMIT 3
SELECT teacher_id, standard_id, SUM(intake) FROM classrooms GROUP BY teacher_id, standard_id ORDER BY teacher_id;
SELECT teacher_id, SUM(intake) FROM classrooms GROUP BY teacher_id ORDER BY teacher_id;
-----------
#CREATE TABLE.
CREATE TABLE basket_a (
a INT PRIMARY KEY,
fruit_a VARCHAR (100) NOT NULL
);
CREATE TABLE basket_b (
b INT PRIMARY KEY,
fruit_b VARCHAR (100) NOT NULL
);
INSERT INTO basket_a (a, fruit_a)
VALUES
(1, 'Apple'),
(2, 'Orange'),
(3, 'Banana'),
(4, 'Cucumber');
INSERT INTO basket_b (b, fruit_b)
VALUES
(1, 'Orange'),
(2, 'Apple'),
(3, 'Watermelon'),
(4, 'Pear');
Code
--------------------------------
Joining-
--------------------------------
Inner Join= if both table column values are match return only match row
SELECT
a,
fruit_a,
b,
fruit_b
FROM
basket_a
INNER JOIN basket_b
ON fruit_a = fruit_b;
--------------------------------
Left Join = show all record of left table, but right table show only match record with table left.
SELECT
a,
fruit_a,
b,
fruit_b
FROM
basket_a
LEFT JOIN basket_b
ON fruit_a = fruit_b;
--------------------------------
Left Outer Join = show those record of left table, which are not match with left table.
SELECT
a,
fruit_a,
b,
fruit_b
FROM
basket_a
LEFT JOIN basket_b
ON fruit_a = fruit_b
WHERE b IS NULL;
--------------------------------
right join = show all record of right table, but from left table show only match record with table right.
select a, fruit_a, b, fruit_b from basket_a
right join basket_b on fruit_a = fruit_b;
--------------------------------
Right Outer Join = show those record of right table, which are not match with right table.
elect a, fruit_a, b, fruit_b from basket_a
right join basket_b on fruit_a = fruit_b where a is null;
--------------------------------
FULL OUTER JOIN = show both table all record, but show match record at first place, if not match with anther row, then row stay empty/null.
select a, fruit_a, b, fruit_b from basket_a
full outer join basket_b
on fruit_a = fruit_b;
--------------------------------
full-
select a, fruit_a, b, fruit_b from basket_a
full outer join basket_b
on fruit_a = fruit_b where a is null or b is null;
--------------------------------
self join
--------------------------------
example of self join
CREATE TABLE employee (
employee_id INT PRIMARY KEY,
first_name VARCHAR (255) NOT NULL,
last_name VARCHAR (255) NOT NULL,
manager_id INT,
FOREIGN KEY (manager_id)
REFERENCES employee (employee_id)
ON DELETE CASCADE
);
INSERT INTO employee(employee_id, first_name, last_name, manager_id)
VALUES
(1, 'Windy', 'Hays', NULL),
(2, 'Ava', 'Christensen', 1),
(3, 'Hassan', 'Conner', 1),
(4, 'Anna', 'Reeves', 2),
(5, 'Sau', 'Norman', 2),
(6, 'Kelsie', 'Hays', 3),
(7, 'Tory', 'Goff', 3),
(8, 'Salley', 'Lester', 3);
select
e.first_name || ' ' || e.last_name employee,
m.first_name || ' ' || m.last_name manager
from
employee e
inner join employee m on m .employee_id = e.manager_id
order by manager;
--------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment