0% found this document useful (0 votes)
12 views1 page

SQL Basics: Table Creation & Data Queries

Uploaded by

bhau1748
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

SQL Basics: Table Creation & Data Queries

Uploaded by

bhau1748
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL Learning Notes - Session 2

1. Table Creation & Data Insertion


CREATE TABLE SALES_2025 (...);
INSERT INTO SALES_2025 (...) VALUES (...), (...), ...;
2. Selecting Data into New Tables
SELECT * INTO NEW_TABLE FROM OLD_TABLE;
SELECT COL1, COL2 INTO NEW_TABLE FROM OLD_TABLE;
SELECT COL1, 'FixedValue' AS NEW_COL INTO NEW_TABLE FROM OLD_TABLE;
3. Filtering Data
Comparison Operators: =, != / <>, >, >=, <, <=
Logical Operators: AND, OR, NOT, IN, NOT IN
Examples:
WHERE COMPANY = 'APPOLO' AND GENDER = 'FEMALE'
WHERE COMPANY IN ('APPOLO', 'GENO') AND GENDER IN ('MALE', 'FEMALE')
4. Arithmetic Operators
Example:
SELECT SPENT_AMOUNT / NO_OF_TRIPS AS SPENT_PER_TRIP FROM MED_2025;
5. Special Operators
BETWEEN: WHERE AGE BETWEEN 30 AND 50
LIKE:
- '%K' → Ends with K
- 'K_____' → Starts with K, 6-letter words
6. GROUP BY & Aggregations
SELECT COMPANY, COUNT(*) AS TOTAL_CUSTOMERS, SUM(SPENT_AMOUNT) AS TOTAL_SPENT
FROM MED_2025 GROUP BY COMPANY;
7. HAVING Clause
Used after GROUP BY to filter aggregate values.
Example:
HAVING COUNT(*) > 50;
8. ORDER BY Clause
Sorts result. Default is ASC. Use DESC for descending order.
9. CASE WHEN Statement
Example (Age Segmentation):
CASE WHEN AGE >= 60 THEN 'OLD-AGE' ... ELSE 'YOUNG AGE' END

Example (Student Grading):


CASE WHEN SCORE >= 85 THEN 'DISTINCTION' ... ELSE 'FAIL' END
10. PIVOT & UNPIVOT
UNPIVOT: Converts columns into rows
PIVOT: Converts rows into columns using CASE + MAX
11. UNION ALL
Combines rows from multiple SELECT statements.
12. SQL Execution Order
1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY
7. TOP / LIMIT

You might also like