AIS SQL Final
AIS SQL Final
SQL Theory
SQL can:
Define and Manipulate Structure: Create, alter, and drop tables and
views.
2. What is a Database?
d. Less secured.
EX: NTFS,FAT
4. What is DBMS?
EX: MySQL.Oracal
6. What is RDBMS?
+-------------------------------------
+-------------------------------------+
| DBMS | RDBMS
|
+-------------------------------------
+-------------------------------------+
| - DBMS stores data as file. | - RDBMS stores data in
tabular |
| - Data elements need to access | form.
|
| individually. | - Multiple data elements
can be |
| - No relationship between data. | accessed at the same
time. |
| - Normalization is not present. | - Data is stored in the
form of |
| | tables which are related
to |
| | each other.
|
| - DBMS does not support | - Normalization is present.
|
| distributed database. | - RDBMS supports
distributed |
| | database.
|
| - It stores data in either a | - It uses a tabular
structure |
| navigational or hierarchical | where the headers are the
|
| form. | column names, and the
rows |
| | contain corresponding
values. |
| - It deals with small quantity | - It deals with large
amount |
| of data. | of data.
|
| - Data redundancy is common in | - Keys and indexes do not
allow |
| this model. | data redundancy.
|
| - It is used for small | - It is used to handle
large |
| organization and deals with | amount of data.
|
| small data. | - All 12 Codd rules are
satisfied. |
| - Not all Codd rules are | - More security measures
provided. |
| satisfied. | - It supports multiple
users. |
| - Security is less. | - Data fetching is fast
because |
| - It supports single user. | of the relational
approach. |
| - Data fetching is slower for | - There exists multiple
levels |
| the large amount of data. | of data security in an
RDBMS. |
| - The data in a DBMS is subject |
|
| to low security levels with |
|
| regards to data manipulation. |
|
| - Low software and hardware |
|
| necessities. |
|
| - Examples: XML, Window Registry, | - Higher software and
hardware |
| Forxpro, dbaseIIIplus etc. | necessities.
|
| | - Examples: MySQL,
PostgreSQL, |
| | SQL Server, Oracle,
Microsoft |
| | Access etc.
|
+-------------------------------------
+-------------------------------------+
Operations in SQL:
1. Arithmetic Operations:
Addition (+), Subtraction (-), Multiplication (*), Division (/)
Modulo (%), Exponentiation (**)
String Concatenation:
3. Relational Operations:
Equal to (=), Not equal to (!= or <>), Greater than (>), Less than
(<)
Greater than or equal to (>=), Less than or equal to (<=)
4. Comparison Operators:
IN, NOT IN: Tests whether a value matches any value in a list.
5. Logical Operations:
6. Subqueries:
7. Aggregate functions like COUNT, SUM, AVG, MIN, MAX that operate
on multiple rows and return a single result.
8. Types of Joins:
9. Data Types:
10. Constraints:
In the Books table, the BookID column serves as the primary key. It
uniquely identifies
each book in the table. A primary key ensures that each entry in a
table is unique and cannot be null.
It helps in indexing and efficiently retrieving specific records.
SQL PRACTICALS
USE Student;
-----------------------------------------------------------------
DML (manipulate data present in the DB)
-- Updating Data:
UPDATE students
SET age = 21
WHERE first_name = 'Sumanth';
UPDATE students
SET email = '[email protected]'
WHERE student_id = 2;
-- Deleting Data:
-- 5. Delete Record
DELETE FROM students WHERE first_name = 'Jane';
-- Deletes all records of students named 'Jane'.
-----------------------------------------------------------------
DDL : Data Definition Language
-- DROP TABLE
-- Explanation: Drops (deletes) the entire table from the database.
-- ALTER TABLE
-- Explanation: Modifies the table structure, like adding or
dropping columns.
-- Example (Adding a new column named city):
ALTER TABLE students ADD COLUMN city VARCHAR(50);
-- TRUNCATE TABLE
-- Explanation: Removes all the records/data from the table while
keeping the table structure intact.
-----------------------------------------------------------------
DCL : Data Control Language
—In MySQL, the GRANT and REVOKE statements are used to manage user
privileges or permissions within the database.
—Let's assume you want to grant or revoke certain privileges for a
specific user on the students table.
—For instance, granting SELECT, INSERT, UPDATE, and DELETE
privileges for a user named my_user to the students table:
START TRANSACTION;
— Explanation:
— START TRANSACTION: Begins a new transaction.
— COMMIT: Saves the changes made during the transaction to the
database permanently.
-- Start a transaction
START TRANSACTION;
— Explanation:
— ROLLBACK: Reverts the changes made during the transaction,
restoring the database to its
-- state before the transaction began.
-----------------------------------------------------------------
Querying Data
-- 1. Select All Records
SELECT * FROM students;
-- Retrieves all records from the students table.
-- 3. Filter by Condition
SELECT * FROM students WHERE age > 25;
-- Retrieves records where the age is greater than 25.
-- 4. Order by Ascending
SELECT * FROM students ORDER BY age ASC;
-- Retrieves all records, sorted by age in ascending order.
-- 5. Order by Descending
SELECT * FROM students ORDER BY age DESC;
-- Retrieves all records, sorted by age in descending order.
-- 6. Count Records
SELECT COUNT(*) FROM students;
-- Counts the total number of records in the students table.
-- 7. Average Age
SELECT AVG(age) FROM students;
-- Calculates the average age of all students.
-- 8. Maximum Age
SELECT MAX(age) FROM students;
-- Retrieves the maximum age among the students.
-- 9. Minimum Age
SELECT MIN(age) FROM students;
-- Retrieves the minimum age among the students.
-- 13. GROUP BY
-- Example 1: Group students by gender and count the number of
students in each gender group.
SELECT *
FROM students
LIMIT 3;
SELECT *
FROM students
ORDER BY age
LIMIT 5;
SELECT first_name
FROM students
ORDER BY first_name DESC;
-- Example 2: Display last names and ages, sorted by age in
ascending order.
SELECT *
FROM students
WHERE first_name LIKE '%oo%';
SELECT *
FROM students
WHERE last_name LIKE '_j%';
-- 17. IN (Search)
-- Example 1: Retrieve students whose ages are 20, 22, or 29.
SELECT *
FROM students
WHERE age IN (20, 22, 29);
-- Example 18: Get students whose first names are either 'John' or
'Jane'.
SELECT *
FROM students
WHERE first_name IN ('John', 'Jane');
SELECT *
FROM students
WHERE age > 25;
SELECT *
FROM students
WHERE last_name > 'D';
-- 20. <> (Not Equal)
-- Example 1: Get students whose age is not 20.
SELECT *
FROM students
WHERE age <> 20;
SELECT *
FROM students
WHERE gender <> 'Male';
-- 21. IS NULL
-- Example 1: Find students without an email address.
SELECT *
FROM students
WHERE email IS NULL;
SELECT *
FROM students
WHERE last_name IS NULL;
SELECT *
FROM students
WHERE email IS NOT NULL;
-- Example 2: Retrieve students with non-NULL first names.
SELECT *
FROM students
WHERE first_name IS NOT NULL;
-- 23. BETWEEN
-- Example 1: Retrieve students aged between 20 and 25.
SELECT *
FROM students
WHERE age BETWEEN 20 AND 25;
-- Example 2: Find students with IDs between 3 and 7.
SELECT *
FROM students
WHERE student_id BETWEEN 3 AND 7;
-- 24. SUM
-- Example 1: Calculate the total age of all students.
-- 25. COUNT
-- Example 1: Count the total number of students.
-- 26. MAX
-- Example 1: Find the maximum age among students.
-- 27. MIN
-- Example 1: Find the minimum age among students.
-- 28. AVERAGE
-- Example 1: Calculate the average age of all students.
-- 29. HAVING
-- Example 1: Get genders having more than two occurrences.