INTRODUCTION TO SQL Mrs. V. JAYAVANI., M.S(IT & M) Assistant Professor Department of Computer Science
Introduction to SQL Data Definition Commands – Data Manipulation Commands – SELECT Queries Structured Query Language (SQL) is the standard language for managing and manipulating relational databases. Whether you're building a new application or maintaining existing systems, mastering SQL is essential. This presentation covers the fundamental SQL commands you'll use daily: DDL commands that shape your database structure, DML commands that handle your data, and SELECT queries that retrieve information. By the end, you'll understand how to create tables, modify data, and write powerful queries to extract exactly what you need.
Data Definition Language (DDL) DDL commands form the structural foundation of your database. Think of DDL as the architectural blueprint—it defines how your data will be organized, but doesn't touch the actual data itself. These commands give you complete control over creating, modifying, and removing database objects like tables, indexes, and constraints. CREATE Build new tables and database objects from scratch ALTER Modify existing table structures by adding or changing columns DROP Permanently delete tables and their data TRUNCATE Remove all records while keeping table structure intact RENAME Change table names for better organization DDL commands are powerful and permanent. When you DROP a table, it's gone—along with all its data. Always double-check before executing DDL statements in production environments.
DDL Examples in Action Creating a Table CREATE TABLE Students ( Student_ID INT PRIMARY KEY, Student_Name VARCHAR(50), Age INT, Department VARCHAR(30)); This creates a Students table with four columns. The Student_ID serves as the primary key, ensuring each student has a unique identifier. Modifying Structure ALTER TABLE Students ADD Email VARCHAR(50); The ALTER command adds a new Email column to our existing table without affecting current data. Removing All Data TRUNCATE TABLE Students; TRUNCATE quickly removes all records but preserves the table structure, ready for new data. It's faster than DELETE for clearing large tables. Deleting the Table DROP TABLE Students; DROP completely removes the table and all its data. Use with extreme caution—this action cannot be undone.
Data Manipulation Language (DML) While DDL shapes your database structure, DML is where the action happens. These commands allow you to insert, update, delete, and retrieve the actual data stored in your tables. Unlike DDL, DML commands work with the content inside your tables without changing the table structure itself. INSERT Add new records to your tables UPDATE Modify existing records with new values DELETE Remove specific records from tables SELECT Retrieve and display data from tables Key Difference: DDL changes structure (like building a house), while DML changes content (like arranging furniture inside). Most of your daily SQL work will involve DML commands, especially SELECT queries.
DML Examples in Action 1 Inserting New Records INSERT INTO Students VALUES (1, 'Priya', 19, 'Computer Science'); The INSERT command adds a new student record to the table. You can insert one row at a time or multiple rows in a single statement. Always ensure your values match the column order and data types defined in your table. 2 Updating Existing Data UPDATE Students SET Age = 20 WHERE Student_ID = 1; UPDATE modifies existing records. The WHERE clause is crucial—it specifies which records to update. Without it, you'd update every row in the table! Here, we're updating only the student with ID 1. 3 Deleting Records DELETE FROM Students WHERE Student_ID = 1; DELETE removes specific records from your table. Like UPDATE, the WHERE clause determines which records to delete. Be extremely careful—omitting WHERE deletes everything! Unlike TRUNCATE, DELETE can be rolled back if you're using transactions.
SELECT Queries: Retrieving Your Data The SELECT command is your most powerful tool for extracting information from databases. It's like asking questions to your data—and getting precise answers. SELECT queries can range from simple requests for all data to complex filtering and sorting operations. Basic SELECT Syntax SELECT column1, column2 FROM table_name WHERE condition; Retrieve Everything SELECT * FROM Students; The asterisk (*) is a wildcard that selects all columns. Perfect for exploring data, but use specific column names in production for better performance. Select Specific Columns SELECT Student_Name, Department FROM Students; Specify only the columns you need. This improves query performance and makes results easier to read, especially with large tables. Filter with WHERE SELECT * FROM Students WHERE Department='Computer Science'; The WHERE clause filters results based on conditions. Here, we're retrieving only Computer Science students. You can combine multiple conditions using AND and OR operators.
Advanced SELECT Techniques Sorting with ORDER BY SELECT * FROM Students ORDER BY Student_Name ASC; ORDER BY sorts your results. Use ASC for ascending (A-Z) or DESC for descending (Z-A) order. You can sort by multiple columns for more precise ordering. Finding Unique Values SELECT DISTINCT Department FROM Students; DISTINCT eliminates duplicate values, showing each unique entry only once. Perfect for discovering what values exist in a column. Pattern Matching with LIKE SELECT * FROM Students WHERE Student_Name LIKE 'P%'; LIKE enables pattern matching. The % wildcard represents any characters—so 'P%' finds names starting with P. Use '%P%' to find P anywhere in the name. Aggregating with GROUP BY SELECT Department, COUNT(*) FROM Students GROUP BY Department; GROUP BY combines rows that share common values, often used with aggregate functions like COUNT, SUM, or AVG to analyze data by category. These advanced SELECT techniques transform simple queries into powerful data analysis tools. Combining ORDER BY, DISTINCT, LIKE, and GROUP BY allows you to answer complex business questions efficiently.
SQL Commands Summary DDL Commands Define Database Structure CREATE – Build new tables and objects ALTER – Modify existing table structure DROP – Delete tables permanently TRUNCATE – Remove all records, keep structure Use DDL to architect your database foundation. DML Commands Manipulate Data Content INSERT – Add new records UPDATE – Modify existing records DELETE – Remove specific records SELECT – Retrieve and query data Use DML for day-to-day data operations. SELECT Queries Retrieve and Filter Data Filter results with WHERE conditions Sort output using ORDER BY Find unique values with DISTINCT Match patterns using LIKE Aggregate data with GROUP BY Master SELECT to unlock your data's full potential. SQL proficiency comes from practice. Start with simple queries and gradually incorporate more advanced techniques. Remember: DDL builds your structure, DML manages your data, and SELECT reveals your insights. With these fundamentals, you're ready to work with any relational database system.