SQL- Data Manipulation Language_Concepts (1) (1)
SQL- Data Manipulation Language_Concepts (1) (1)
A B C D
A1 B1 USA 10
A2 B1 INDIA 20
A3 B2 GERMANY 30
A4 B2 INDONESIA 40
A B C D
A1 B1 USA 10
SELECT * Retrieve all columns from a table SELECT * FROM TEST; A2 B1 INDIA 20
A3 B2 GERMANY 30
A4 B2 INDONESIA 40
A B C D
SELECT * LIMIT 2 Retrieve all columns from a table SELECT * FROM TEST LIMIT 2; A1 B1 USA 10
A2 B1 INDIA 20
A B
A1 B1
SELECT <columns> Retrieve specific columns from a table SELECT A, B FROM TEST; A2 B1
A3 B2
A4 B2
B
SELECT DISTINCT Retrieve unique rows of column(s) from a table SELECT DISTINCT B FROM TEST; B1
B2
A B C D
A1 B1 USA 10
INSERT INTO TEST VALUES ('A5', 'B2', A2 B1 INDIA 20
INSERT Put new values to the table
'UK', 25); A3 B2 GERMANY 30
A4 B2 INDONESIA 40
A5 B2 UK 25
A B C D
A1 B1 USA 5
A2 B1 INDIA 15
UPDATE Update one or more values in the table UPDATE TEST SET D = D - 5;
A3 B2 GERMANY 25
A4 B2 INDONESIA 35
A5 B2 UK 20
A B C D
A1 B1 USA 5
A2 B1 INDIA 15
DELETE Delete one record from table at a time DELETE FROM TEST WHERE A = A3;
A3 B2 GERMANY 30
A4 B2 INDONESIA 35
A5 B2 UK 20
TEST
A B C D
A1 B1 USA 5
A2 B1 INDIA 15
A4 B2 INDONESIA 35
A5 B2 UK 20
A B C D
WHERE Fetch data based on a condition SELECT * FROM TEST WHERE B = 'B2' A4 B2 INDONESIA 35
A5 B2 UK 20
A B C D
IN Allows to specify multiple conditions inside a where clause SELECT * FROM TEST WHERE C IN ('USA', 'UK') A1 B1 USA 5
A5 B2 UK 20
A B C D
Search for a specific pattern with a where clause. % to match zero, one
LIKE % SELECT * FROM TEST WHERE C LIKE 'IND%' A2 B2 INDIA 15
or more characters.
A4 B2 INDONESIA 35
A B C D
Search for a specific number of characters using '_'. To find a value with
LIKE _ SELECT * FROM TEST WHERE C LIKE '___'
3 characters use ' ' A1 B1 USA 5
A B C D
SELECT * FROM TEST WHERE D BETWEEN 15 A2 B2 INDIA 15
BETWEEN Fetch the data within specific range
AND 35 A4 B2 INDONESIA 35
A5 B2 UK 20
A B C D
AND Fetch the record(s) if all the conditions specified by AND are TRUE SELECT * from TEST WHERE B = 'B2' AND D > 15 A4 B2 INDONESIA 35
A5 B2 UK 20
A B C D
Fetch the record(s) if atleast one of the conditions specified by OR is/are A2 B1 INDIA 15
OR SELECT * from TEST WHERE B = 'B2' OR D > 10
TRUE A4 B2 INDONESIA 35
A5 B2 UK 20
B D_TOTAL
SUM() Calculate the sum of all selected columns. Works on SELECT B, SUM(D) AS D_TOTAL FROM TEST
B1 20
+ GROUP BY numeric columns only GROUP BY B
B2 55
B D_AVERAGE
AVG() Calculate average or arithmetic mean of the selected SELECT B, AVG(D) AS D_AVERAGE FROM TEST
B1 10
+ GROUP BY numerical column
B2 27.5
B D_MIN
MIN() Calculate the minimum value of the selected
SELECT B, MIN(D) AS D_MIN FROM TEST B1 5
+ GROUP BY numerical column
B2 20
B D_MAX
MAX() Calculate the maximum value of the selected SELECT B, MAX(D) AS D_MAX FROM TEST
B1 15
+ GROUP BY numerical column GROUP BY B
B2 35
CNT
COUNT(*) Count all the rows in the table SELECT COUNT(*) AS CNT FROM TEST;
4
CNT
COUNT(DISTINCT) Counts the distinct entries in a column SELECT COUNT(DISTINCT B) AS CNT FROM TEST;
2
B B_COUNT
COUNT() Calculate the number of rows in the database table. It SELECT B, COUNT(B) AS B_COUNT FROM TEST
B1 2
+ GROUP BY works on both numeric and non-numeric column(s) GROUP BY B
B2 2
A B C D
A4 B2 INDONESIA 35
ORDER BY Sort the data in either ascending or descending order SELECT * FROM TEST ORDER BY D DESC A5 B2 UK 20
A2 B1 INDIA 15
A1 B1 USA 5
TABLE Y
P Ǫ R
A1 100 10
A2 200 20
A3 300 30
A5 400 40
TEST
A B C D
A1 B1 USA 5
A2 B2 INDIA 15
A4 B3 INDONESIA 35
A5 B2 UK 20
Used to fetch common records from two tables using a common SELECT * X.A X.B X.C Y.P Y.Ǫ Y.R
column present in both the tables FROM X INNER JOIN Y ON X.A = Y.P A1 100 10 A1 100 10
INNER JOIN
A2 200 20 A2 200 20
A3 300 30 A3 300 30
A B C D A Ǫ
A1 100 10 A1 100 10
Used to fetch all the records from the right table even if there are SELECT *
RIGHT JOIN A2 200 20 A2 200 20
no matches in the left table FROM X RIGHT JOIN Y ON X.A = Y.P
A3 300 30 A3 300 30
NULL NULL NULL A5 400 40
A B C
A1 100 10
A2 200 20
UNION ALL fetches all the records in the two tables. The tables SELECT * FROM X A3 300 30
UNION ALL are put one on top of the other and the column names are got UNION ALL A4 400 40
from the top table. SELECT * FROM Y; A1 100 10
A2 200 20
A3 300 30
A5 400 40
A B C
A1 100 10
SELECT * FROM X
UNION works the same as UNION ALL, but fetches only the A2 200 20
UNION UNION
distinct records A3 300 30
SELECT * FROM Y;
A4 400 40
A5 400 40
It is used to join a table to itself. T1.A T1.B T1.C T2.A T2.B T2.C
Generally used for comparing the rows within the same table A1 100 10 A1 100 10
SELECT * FROM
SELF JOIN A2 200 20 A2 200 20
X AS T1 JOIN X AS T2 WHERE T1.A = T2.A
A3 300 30 A3 300 30
A4 400 40 A4 400 40
TABLE
A B C D
A1 B1 X1 10
A1 B1 X2 10
A1 B2 X3 30
A1 B2 X4 20
A2 B1 X5 40
A2 B1 X6 60
A2 B2 X7 50
A2 B2 X8 80
A B C D CNT
A1 B1 X1 10 4
SELECT A, A1 B1 X2 10 4
B, A1 B2 X3 30 4
COUNT() Let's try counting the number of records at the level of A C, A1 B2 X4 20 4
D, A2 B1 X5 40 4
COUNT(*) OVER (PARTITION BY A) AS CNT FROM TABLE; A2 B1 X6 60 4
A2 B2 X7 50 4
A2 B2 X8 80 4
A B C D SUM
A1 B1 X1 10 20
SELECT A, A1 B1 X2 10 20
B, A1 B2 X3 30 50
SUM() Let's try to sum D at the level of A and B C, A1 B2 X4 20 50
D, A2 B1 X5 40 100
SUM(D) OVER (PARTITION BY A, B) AS SUM FROM TABLE; A2 B1 X6 60 100
A2 B2 X7 50 130
A2 B2 X8 80 130
A B C D ROWNUM
A1 B2 X3 30 1
SELECT A, A1 B2 X4 20 2
B, A1 B1 X1 10 3
Adds a row number at the level and order specified. Let's add
C,
ROW_NUMBER() row numbers at the level of A and order by D in the A1 B1 X2 10 4
D,
descending order. A2 B2 X8 80 1
ROW_NUMBER() OVER (PARTITION BY A ORDER BY D DESC)
AS ROWNUM FROM TABLE; A2 B1 X6 60 2
A2 B1 X5 50 3
A2 B2 X7 40 4
A B C D RNK
A1 B1 X1 10 1
SELECT A, A1 B1 X2 10 1
B, A1 B2 X4 20 3
Adds a rank at the level and order specified. Let's add row
C,
RANK() numbers at the level of A and order by D in the ascending A1 B2 X3 30 4
D,
order. A2 B1 X5 40 1
RANK() OVER (PARTITION BY A ORDER BY D ASC) AS RNK
FROM TABLE; A2 B2 X7 50 2
A2 B1 X6 60 3
A2 B2 X8 80 4
A B C D D_RNK
A1 B1 X1 10 1
SELECT A, A1 B1 X2 10 1
B, A1 B2 X4 20 2
Adds a dense rank at the level and order specified. Let's add
C,
DENSE_RANK() row numbers at the level of A and order by D in the ascending A1 B2 X3 30 3
D,
order. A2 B1 X5 40 1
DENSE_RANK() OVER (PARTITION BY A ORDER BY D ASC) AS
D_RNK FROM TABLE; A2 B2 X7 50 2
A2 B1 X6 60 3
A2 B2 X8 80 4