170 likes | 305 Views
SQL. Data Manipulation Language (DML) SELECT. SQL DML - SELECT. SELECT [DISTINCT|ALL] {* | [col-expr][,...] FROM table-name [alias] [,...] [WHERE condition] ORDER BY colm [, colm]. SQL DML - SELECT. SELECT attributes (or calculations: +, -, /, *) FROM relation
E N D
SQL • Data Manipulation Language (DML) • SELECT
SQL DML - SELECT • SELECT [DISTINCT|ALL] {* | [col-expr][,...] FROM table-name [alias] [,...] [WHERE condition] ORDER BY colm [, colm]
SQL DML - SELECT • SELECT attributes (or calculations: +, -, /, *) FROM relation • SELECT DISTINCT attributes FROM relation
Examples • SELECT stuname FROM student; • SELECT stuid, stuname, credit FROM student; • SELECT stuid, stuname, credit+10 FROM student; • SELECT DISTINCT major FROM student;
SQL DML - SELECT • SELECT attributes (or * wild card) FROM relation WHERE condition
Examples • SELECT * FROM student; • SELECT stuname, major, credit FROM student WHERE stuid = ‘S114’; • SELECT * FROM faculty WHERE dept = ‘MIS’;
SELECT - WHERE condition • AND OR • NOT IN • NOT IN BETWEEN • IS NULL IS NOT NULL • LIKE '%' multiple characters • LIKE ‘_’ single characters
Examples • SELECT * FROM faculty WHERE dept = ‘MIS’ AND rank = ‘full professor’; • SELECT * FROM faculty WHERE dept = ‘MIS’ OR rank = ‘full professor’; • SELECT * FROM faculty WHERE dept = ‘MIS’ NOT rank = ‘full professor’;
SELECT * FROM class WHERE room LIKE ‘B_S%’; • SELECT * FROM class WHERE room NOT LIKE ‘BUS%’; • SELECT productid, productname FROM inventory WHERE onhand BETWEEN 50 and 100;
SELECT companyid, companyname FROM company WHERE companyname BETWEEN ‘G’ AND ‘K’; • SELECT productid, productname FROM inventory WHERE onhand NOT BETWEEN 50 and 100; • SELECT companyid, companyname FROM company WHERE companyname NOT BETWEEN ‘G’ AND ‘K’;
SELECT facname FROM faculty WHERE dept IN (‘MIS’, ‘ACT’); • SELECT facname FROM faculty WHERE rank NOT IN (‘assistant’, ‘lecture’); • SELECT customername FROM customer WHERE emailadd IS NOT NULL;
SELECT customername FROM customer WHERE creditlimit IS NULL;
SELECT - ORDER BY • ORDER BY • ORDER BY ... DESC
Examples • SELECT facname, rank FROM faculty ORDER BY facname; • SELECT facname, rank FROM faculty ORDER BY rank DESC, facname;
SELECT - JOIN Tables • Multiple tables in FROM clause • MUST have join conditions!!!
Examples • SELECT stuname, grade FROM student, enrollment WHERE student.stuid = enrollment.stuid;
SELECT enrollment.course#, stuname, major FROM class, enrollment, student WHERE class.course# = enrollment.course# AND enrollment.stuid = student.stuid AND facid = ‘F114’ ORDER BY enrollment.course#;