SQL
Example Tables: Indha workshop full-a, namma 2 example tables use pannuvom: Students matrum Courses.
| StudentID | FirstName | City | Age |
|---|---|---|---|
| 101 | Kavin | Chennai | 20 |
| 102 | Priya | Madurai | 21 |
| 103 | Arun | Chennai | 22 |
| 104 | Divya | Coimbatore | 20 |
| 105 | Ram | Madurai | 21 |
| CourseID | CourseName | StudentID | Marks |
|---|---|---|---|
| C1 | SQL | 101 | 85 |
| C2 | Python | 102 | 90 |
| C1 | SQL | 103 | 70 |
| C3 | Java | 101 | 80 |
| C2 | Python | 104 | 95 |
| C1 | SQL | 105 | NULL |
Database (DB): Idhu data-va store panni vekkura oru digital idam. Udharanam: Unga phone contacts, bank account details.
SQL (Structured Query Language): Idhu andha database kitta pesuradhukku use aagura oru 'mozhi' (language). Data-va paaka, serkka, azhikka, maatha use aagum.
RDBMS (Relational Database): Idhu data-va 'Tables' (like Excel sheets) la store pannum. E.g., MySQL, PostgreSQL, MS SQL Server.
Table: Idhu rows (varisaigal) and columns (niralkal) konda data set.
SELECT: Enna columns venum nu solradhukku.
FROM: Endha table-la irundhu venum nu solradhukku.
WHERE: Enna condition (nibanthanai) base panni venum nu solradhukku.
SELECT*FROMStudents;
(* 'asterisk' na 'ella columns' nu artham)
SELECTFirstName, CityFROMStudents;
SELECT*FROMStudentsWHERECity ='Chennai';
SELECT*FROMStudentsWHEREAge >20;
Chennai-la irukkura, 20 vayasulla students.
SELECT*FROMStudentsWHERECity ='Chennai'ANDAge =20;
Chennai 'alladhu' Madurai-la irukkura students.
SELECT*FROMStudentsWHERECity ='Chennai'ORCity ='Madurai';
Short-a eppadi eludhalam (OR-ku badhil):
SELECT*FROMStudentsWHERECityIN('Chennai','Madurai');
'K' la aarambikura paer (FirstName) konda students.
SELECT*FROMStudentsWHEREFirstNameLIKE'K%';
(% na 'edhu venalum irukkalam' nu artham)
Age 20-kum 21-kum naduvula (rendum serthu).
SELECT*FROMStudentsWHEREAgeBETWEEN20AND21;
Students-a avanga Age (vayasu) padi varisai paduthu (chinnadhula irundhu perusu - ASC).
SELECT*FROMStudentsORDER BYAge;
(Default-a 'ASC' - Ascending order)
Students-a avanga Age padi 'DESC' (Descending) order-la varisai paduthu.
SELECT*FROMStudentsORDER BYAgeDESC;
Mudhal 3 students-a mattum kaatu.
SELECT*FROMStudentsLIMIT3;
Students irukkura oorgal (Cities) pattiyal (duplicate illama).
SELECTDISTINCTCityFROMStudents;
COUNT: Ennikkai (count)
SUM: Kootuthogai (total)
AVG: Sarasari (average)
MAX: Adhigapatcha (maximum)
MIN: KuraINDhapatcha (minimum)
Mothama ethana students irukkanga?
SELECTCOUNT(*)FROMStudents;
Chennai-la motham ethana students?
SELECTCOUNT(*)FROMStudentsWHERECity ='Chennai';
Motha marks (ellam courses serthu) evlo?
SELECTSUM(Marks)FROMCourses;
Python course-oda sarasari (AVG), adhigapatcha (MAX), kuraINDhapatcha (MIN) marks.
SELECTAVG(Marks),MAX(Marks),MIN(Marks)FROMCoursesWHERECourseName ='Python';
Note: Aggregate functions (SUM, AVG, etc.) `NULL` values-a ignore pannidum. `COUNT(*)` mattum `NULL` row-ayum serthu ennum.
`Courses` table-la, `COUNT(Marks)` 5 nu varum, aana `COUNT(*)` 6 nu varum (Ram-oda row serthu).
SELECTCOUNT(*),COUNT(Marks)FROMCourses;
GROUP BY: Idhu data-va palveru 'groups'-a pirikkum. (E.g., Ovvoru ooor (city) group, ovvoru course group).
HAVING: Idhu `GROUP BY` panna data-va filter panna use aagum (WHERE madhiri, aana `GROUP BY`-ku apparam).
Ovvoru city-layum ethana students irukkanga?
SELECTCity,COUNT(*)FROMStudentsGROUP BYCity;
Ovvoru city-la, ovvoru age-layum ethana students?
SELECTCity, Age,COUNT(*)FROMStudentsGROUP BYCity, Age;
Ovvoru course-kum sarasari (AVG) mark evlo?
SELECTCourseName,AVG(Marks)FROMCoursesGROUP BYCourseName;
Sarasari (AVG) mark 85-ku maela irukkura courses mattum.
SELECTCourseName,AVG(Marks)FROMCoursesGROUP BYCourseNameHAVINGAVG(Marks) >85;
Mukkiyamaana kelsi: WHERE vs HAVING?
WHERE: Data-va group panradhukku *munnadi* filter pannum (normal columns mela).
HAVING: Data-va group pannadhukku *apparam* filter pannum (Aggregate function results mela).
JOIN: Idhu thaan SQL-la romba mukkiyamaana concept. Rendu (alladhu pala) tables-a oru 'common column' (podhuvaana column) vechi serkkuradhu.
Inga, namma `Students` table-ayum `Courses` table-ayum `StudentID` vechi serkka porom.
Rendu table-layum match aagura data-va mattum eduthu serkkum.
Student paer (Students table) matrum avanga edutha course (Courses table) serthu paaka.
SELECTS.FirstName, S.City, C.CourseName, C.MarksFROMStudents SINNER JOINCourses CONS.StudentID = C.StudentID;
(Inga `S` and `C` enbadhu tables-ku namma kudukura 'short names' (alias))
Idhadhu pakkam (LEFT) irukkura table (Students) oda ella data-vum varum. Valadhu pakkam (Courses) match irundha, adha kaatum, illana `NULL` kaatum.
Ella students-ayum kaatu, avanga course eduthurundha adhayum kaatu (course edukkalana `NULL` varum).
SELECTS.FirstName, C.CourseName, C.MarksFROMStudents SLEFT JOINCourses CONS.StudentID = C.StudentID;
(Indha example-la, `Students` table-la irukkura ellarume `Courses` table-layum irukuradhala, result `INNER JOIN` madhiriye irukkum. Aana oru student course edukalana, LEFT JOIN la avanga paer `NULL` course-oda varum.)
Warning: Indha commands data-va maathidum. Romba gavanama use pannanum!
Pudha oru student-a serkka.
INSERT INTOStudents (StudentID, FirstName, City, Age)VALUES(106,'Mani','Trichy',22);
Arun (ID 103) oda city-a 'Coimbatore'-ku maatha.
UPDATEStudentsSETCity ='Coimbatore'WHEREStudentID =103;
(GAVANAM: `WHERE` clause illama `UPDATE` pannadhaenga! Ella rows-um maaridum!)
Mani (ID 106) student record-a azhikka.
DELETE FROMStudentsWHEREStudentID =106;
(GAVANAM: `WHERE` clause illama `DELETE` pannadhaenga! Ella data-vum azhinjirum!)
Pudusa oru table (e.g., `Teachers`) urvakka.
CREATE TABLETeachers ( TeacherIDINT, TeacherNameVARCHAR(100), DepartmentVARCHAR(50) );
(`INT` na number, `VARCHAR` na text/string)
Oru table-a full-a azhikka (data + structure).
DROP TABLE Teachers;