You are on page 1of 5

SQL FUNDAMENTALS (BY PRAVEEN SIR):

DATABASE: Collection of data is known as database.

RDBMS: Relational Database Management System.

1. Relation in the form of table. [Relational]


2. It is collection of data. (Data is an unorganized information) [database]
3. It describes way to manage data. (To minimize redundancy or to reduce repetition)
[management system].

RDBMS: Concept

SQL: Language (Structured Query Language)

MYSQL: Server

FIELDS OR ATTRIBUTES: Columns are also known as Fields or attributes.

TUPLES: Rows are known as tuples.

RECORDS: Rows with data inserted in them are known as records.

CARDINALITY: The number of rows in a table is known as Cardinality.

DEGREE: The number of columns in a table is known as a Degree.

PRIMARY KEY: Unique and Not null.

FOREIGN KEY: Also known as reference key.

• It can be same
• It can be null.
Primary key column should be must in a table.

There can be more than 1 foreign key column in a table.

DDL: Database and Tables.

DML and DQL: Data inside the tables.

DCL: Permission.

SOME COMMANDS:

CREATE DATABASE demo;

SHOW DATABASES;

USE demo;
CREATE TABLE person (col-name datatype constraints);

SHOW TABLES;

DROP TABLE person;

DROP: Structure of table will be dropped along with relationship, keys, access privilege.

ALTER:

i. Change table name:


Alter table person RENAME to employee.
ii. Add a column:

Alter table employee add location varchar(25) not null;

iii. Remove a column:

Alter table employee drop column location;

DML COMMANDS:

• INSERT:

INSERT INTO employee VALUES (101,’NISHI’,’BHOPAL’);

INSERT INTO employee (id, location, age) VALUES (‘POOJA’);

DQL COMMANDS:

SELECT * FROM employee;

SELECT location FROM employee;

SELECT location, age FROM employee;

UPDATE:

UPDATE employee SET name=’Nishi’ WHERE id=102;

DELETE:

Always a full record is deleted.

DELETE FROM employee WHERE id=102;

CLAUSES:

1. WHERE
2. AND
3. OR
4. AS
5. ORDER BY
6. GROUP BY

WHERE: Satisfies a specific condition.

E.g.: SELECT * FROM employee WHERE age>21;


AND:

SELECT * FROM employee WHERE age>20 AND location=’Bhopal’;

OR:

SELECT * FROM employee WHERE age>20 OR location=’Bhopal’;

AS:

SELECT name as First_name FROM employee; (TEMPORARY CHANGES NAME OF COLUMN)

ORDER BY:

SELECT * FROM employee ORDER BY name;

By default ascending order.

SELECT * FROM employee ORDER BY name desc;

DESCENDING: DESC

ASCENDING: ASC

GROUP BY:

SELECT count(name) FROM employee GROUP BY location;

SELECT count(name), location FROM employee GROUP BY location;

FUNCTIONS:

1. MIN
2. MAX
3. AVG
4. SUM
5. COUNT

SELECT MIN (age) FROM employee;

SELECT MAX (age) FROM employee;

SELECT AVG (age) FROM employee;

SELECT SUM (age) FROM employee;

NESTED QUERIES:

SELECT * FROM employee WHERE AGE= (SELECT MIN (age) FROM employee);

Internal query will be executed first.

OPERATORS:

SELECT * FROM employee WHERE id BETWEEN 102 and 105;

SELECT * FROM employee WHERE id IN (102, 104, 106, 108);

SELECT * FROM employee WHERE id NOT IN (102, 104, 106, 108);


SELECT * FROM employee WHERE EXISTS (SELECT id FROM employee WHERE id = 108);

SELECT * FROM employee WHERE name like ‘a%’; (0, 1 or multiple characters).

SELECT * FROM employee WHERE name like ‘_a%’

SELECT * FROM employee WHERE name like ‘%a%’

SELECT * FROM employee WHERE name like ‘%a_’

SELECT * FROM employee LIMIT 3;

SELECT * FROM employee WHERE location=’Bhopal’ LIMIT 2;

JOINS:

Joins are used for combining 2 columns of tables by using common values from both tables.

CROSS JOIN (CARTESIAN PRODUCT):

Column of 1st table is multiplied with all other columns of the other table.

SELECT * FROM person CROSS JOIN address;

INNER JOIN (EQUI JOIN):

SELECT * FROM person INNER JOIN address WHERE person.id = address.id; (WHERE clause is must).

SELECT * FROM person NATURAL JOIN address

OUTER JOIN:

SELECT * FROM person LEFT/RIGHT OUTER JOIN address ON (person.id = address.id);

You might also like