Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL (Structured Query Language)

Similar presentations


Presentation on theme: "SQL (Structured Query Language)"— Presentation transcript:

1 SQL (Structured Query Language)

2 SQL SQl is a query language and is supported by almost all major database management systems. It can be used in a database system interactively using a graphical user interface or prompt so you type your SQL queries or commands and get the result back or SQL can be embedded in the program. SQL is a declarative language you write simple queries that say what you want out of the database and queries do not need to describe how to get the data out of the database. The language is based on the relational algebra. The declarative nature of the language leads to a component of the database system called query optimizer to be so important. What the query optimizer does is it takes the query written in SQL and it figures out the best way, the fastest way to execute it.

3 Select Statement take a look in a little more detail at select statement
Select statement consists of three basic clauses; select clause, from clause and where clause. The best order to think of these is actually, first from clause then where clause and then select clause. From identifies the relations you want to query over. Condition is used to combine the relations and to filter the relations. Finally select says you what to return. Relational query languages are compositional meaning when you run a query over relations you get a relation as a result. It doesn’t have any name but the schema of the relation is the set of attributes that are returned.

4 Summery SQl is very prominent, is supported by all major database systems.. It has been standardized over time. It can be used through programs. It can be used interactively. It’s declarative, high-level language. Whose foundations are based on relational algebra.

5 Our sample database College(cName, state, enrollement)
Student(sID, sName,GPA,sizeHS) Apply(sID, cName, major, decision) (students apply to each college for a specific major one time) Underlined attributes are the key attributes. What is a key: key is an attribute or a set of attribute whose value is guaranteed to be unique. Given our database we apply some queries against the database.

6 Queries: 1.find Id, name and GPA of those students whose GPA is greater than 3.6. 2.Find the names of the students and the major for which they applied. ( we want to combine student with apply relation and we have to mention join condition explicitly although in relational algebra would automatically happen in natural join) We can expect even duplicates as a result of query in SQL. However, this is not the case in relational algebra. In relational algebra the default is the set model but in SQL the default is multi-set model. Using Distinct keyword in select clause eliminate any possible duplicates. 3. Find name and GPA of the students whose sizeHS is less than a thousand, they applied to CS in Stanford and we are going to get the decision associated with that.

7 continue… Some notes about the third query(two tables involved, we have the join condition to make sure we are talking about the same student. We are going to filter the results based on the size high school, major and the college to which they’re applying) 4. Find large campuses that have some one applying to this campus in CS.(we are going to join college and apply tables .Again there is join condition to make sure we are talking about the same college and have an enrollment greater than some amount.) Is there any specific observation after running this query against your database, mention if any. 5. we want to get the studentIDs, their name, their GPA, the college they’re applying to and the enrollment of that college. Question: Is there any order in results when we run SQL queries? SQl, at its heart, is unordered model. That means we can get the result of our queries in any order. If we care about the results SQL provides a clause to order the query results


Download ppt "SQL (Structured Query Language)"

Similar presentations


Ads by Google