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

SQL

The document provides an overview of SQL commands including table creation, data insertion, selection, updating, and deletion. It also covers various SQL operators such as comparison, logical, and set operations, as well as different types of joins and their applications. Additionally, it explains the use of aggregation functions and the concept of primary and foreign keys in relational databases.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL

The document provides an overview of SQL commands including table creation, data insertion, selection, updating, and deletion. It also covers various SQL operators such as comparison, logical, and set operations, as well as different types of joins and their applications. Additionally, it explains the use of aggregation functions and the concept of primary and foreign keys in relational databases.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL

- CREATE TABLE

CREATE TABLE player (


name VARCHAR(200),
age INTEGER,
score INTEGER
);

- INSERT VALUES IN TABLE

INSERT INTO
player (name, age, score)
VALUES
("Rakesh", 39, 35),
("Sai", 47, 30);

- SELECT
SELECT name, age
FROM player
WHERE name = "Sai";

- UPDATE

UPDATE
player
SET
score = 100;

- DELETE

DELETE FROM
player
WHERE
name = "Shyam";

- ALTER

ALTER TABLE
player
ADD
jersey_num INT;

COMPARISON OPERATORS
Operator Description
= Equal to
<> Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

EXAMPLE :
SELECT
*
FROM
product
WHERE
category <> "Food";

LIKE OPERATOR ---

example:
SELECT
*
FROM
product
WHERE
name LIKE "Bourbon%";

ending %, starting %, _ for letter


example:
SELECT
*
FROM
product
WHERE
brand LIKE "B__e";

output: blue

LOGICAL OPERATORS - AND,OR,NOT

- IN OPERATOR
SELECT
*
FROM
product
WHERE
brand IN ( "Puma", "Levi's", "Mufti", "Lee", "Denim");

-BETWEEN OPERATOR

SELECT
name,
price,
brand
FROM
product
WHERE
price BETWEEN 1000
AND 5000;

- ORDER BY

SELECT
name,
price,
rating
FROM
product
WHERE
name = "Blue Shirt"
ORDER BY
rating DESC,
price ASC;

- DISTINCT (TO REMOVE DUPLICATES)

SELECT
DISTINCT brand
FROM
product
ORDER BY
brand;

- HAVING (after group by we should use having)

SELECT
name,
COUNT(*) AS half_centuries
FROM
player_match_details
WHERE
score >= 50
GROUP BY
name
HAVING
half_centuries > 1;

- strftime usage

SELECT
strftime('%m', release_date) AS month,
COUNT(*) AS total_movies
FROM
movie
WHERE
strftime('%Y', release_date) = '2010'
GROUP BY
month;

AGGREGATIONS:

SUM , AVG, COUNT, MAX, MIN

SELECT
course.name AS course_name,
MAX(score) AS highest_score
FROM
course

SET OPERATIONS
INTERSECT - PRESENT IN BOTH A AND B
MINUS - PRESENT IN A AND NOT IN B (EXCEPT)
UNION - EITHER IN A OR B
UNION ALL - ALL A AND B WITHOUT REMOVING DUPLICATES

EXAMPLE:
SELECT
actor_id
FROM
cast
WHERE
movie_id = 6
INTERSECT
SELECT
actor_id
FROM
cast
WHERE
movie_id = 15;

JOINS

RELATIONSHIPS - 1 TO 1
1 TO MANY (OR) MANY TO 1
MANY TO MANY

PRIVATE KEY - Unique identification in particular table


FOREIGN KEY - private key in other table is foreign key in present table

Private key :

student_id integer not null primary key; it should be mentioned like this.

foreign key:

FOREIGN KEY (customer_id) REFERENCES customer(id) ON DELETE CASCADE

Example:

CREATE TABLE address(


id INTEGER NOT NULL PRIMARY KEY,
pin_code INTEGER,
door_no VARCHAR(250),
city VARCHAR(250),
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customer(id) ON DELETE CASCADE
);

1. Natural Join
(joins based on common column in both table)

SELECT
review.course_id,
review.content,
review.created_at,
student_course.score
FROM
review NATURAL
JOIN student_course
WHERE
student_course.score > 70;

2. INNER JOIN

(Combines two tables if condition is met)

SELECT student.full_name,
review.content,
review.created_at
FROM student
INNER JOIN review
ON student.id = review.student_id
WHERE review.course_id = 15;

3.LEFT JOIN

(LEFT TABLE WILL COME, THE LEFT AND RIGHT COMMON WILL COME, OTHERS WHICH ARE NOT
COMMON IN RIGHT ARE NULL)

SELECT student.full_name
FROM student
LEFT JOIN student_course
ON student.id = student_course.student_id
WHERE student_course.id IS NULL;

3. RIGHT JOIN

(RIGHT TABLE WILL COME, THE LEFT AND RIGHT COMMON WILL COME, OTHERS WHICH ARE NOT
COMMON IN LEFT ARE NULL)

SELECT course.name,
instructor.full_name
FROM course
RIGHT JOIN instructor
ON course.instructor_id = instructor.instructor_id;

4. FULL JOIN

RESULT OF BOTH RIGHT JOIN + LEFT JOIN

SELECT course.name,
instructor.full_name
FROM course
FULL JOIN instructor
ON course.instructor_id = instructor.instructor_id;
5. CROSS JOIN
(each row from the first table is combined with all rows in the second table.)
Cross Join is also called as CARTESIAN JOIN

SELECT course.name AS course_name,


instructor.full_name AS instructor_name
FROM course
CROSS JOIN instructor;

THESE ALL ARE COMBINING TWO TABLES, TO COMBINE COLUMNS WITHIN THE TABLE WE USE SELF
JOIN.

You might also like