Structured Query Language

Slides:



Advertisements
Similar presentations
SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
Advertisements

OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
Database Design -- Basic SQL
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Chapter 2 Basic SQL SELECT Statements
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
Relational Algebra - Chapter (7th ed )
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
COMP163 Database Management Systems September 18, 2008 Lecture 8 – Sections SQL Queries.
1 Database Systems Basic SQL. 2Outline  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries in SQL  INSERT,
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL SELECT Statements Lecture
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
1 COMP 1100 Basic SQL David J. Stucki. Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
CS580 Advanced Database Topics
Writing Basic SQL SELECT Statements
Basic select statement
Chapter 4 Basic SQL.
6/22/2018.
Retrieval Queries in SQL(DML)
376a. Database Design Dept. of Computer Science Vassar College
11/9/2018.
Writing Basic SQL SELECT Statements
Session - 6 Sequence - 2 SQL: The Structured Query Language:
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
SQL-99: Schema Definition, Constraints, and Queries and Views
Restricting and Sorting Data
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Restricting and Sorting Data
Lab 2: Retrieving Data from the Database
Presentation transcript:

Structured Query Language

Basic Queries in SQL SQL has one basic statement for retrieving information from a database The Select Statement

Basic SELECT Statement In its simplest form, a SELECT statement must include the following: A SELECT clause, which specifies the columns to be displayed A FROM clause, which specifies the table containing the columns listed in the SELECT clause

Selecting All Columns, All Rows You can display all columns of data in a table by following the SELECT keyword with an asterisk (*). Example: Select * from department;

Selecting Specific Columns, All Rows You can use the SELECT statement to display specific columns of the table by specifying the column names, separated by commas. Example: Select fname , lname from employee;

ARITHMETIC EXPRESSIONS Arithmetic operators such as +, -, *, / may be used in SQL statements. ex. SELECT fname, salary+100 FROM employee;

Duplicate Rows The default display of queries is all rows, including duplicate rows To eliminate dublicate rows we use DISTINCT keyword in select clause

Duplicate rows Example : Select distinct salary from employee

Column Alias Column aliases exist to help organizing output. The syntax: SELECT column AS column_alias FROM table Ex. How to use alias SELECT Lname As employee FROM employee; Or: SELECT Lname employee Or: SELECT Lname “Employee Record”

Concatenation and Literals For string data types, the concatenate operator || can be used in a query to append two string values. Literals refers to a fixed data value such as string, number or date. It will be viewed one time in each row.

Example Select SSN, Fname || ' ' || Minit || ' ' || Lname From Employee;

Ordering of Query Results SQL allows the user to order the rows in the result of a query by the values of one or more attributes, using the ORDER BY clause. The default order is in ascending order of values. We can specify the keyword DESC if we want to see the result in a descending order of values. The keyword ASC can be used to specify ascending order explicitly. if we want descending order on DNAME and ascending order on LNAME, FNAME the order by clause will be: ORDER BY DNAME DESC, LNAME ASC, FNAME ASC

Limiting Row Selection The queries we wrote so far display all the rows from a table. When you select information from the database sometimes it is desirable to limit the rows that you select. For example if there were 5000 rows of data in the employee table and you wanted to look at the details of only one employee then you would not want to have to select all 5000 rows. You would only want to retrieve the details of the employee you were interested in. You can reduce the number of rows selected by use of the where clause. The where clause is part of the select statement and is optional. The where clause limits the number of rows retrieved according to some criteria The where clause, if used, must follow the FROM clause. It contains a condition, which rows must meet in order to be displayed. Select columns From table Where certain_condition;

Example For example if you want to display a list of all those employees who are working in department 4, the SQL statement will be Select ssn, fname, dno From employee Where dno = 4;

Comparison Operators =, < , <=,>, >= and <> NOT AND AND and OR join two or more conditions in a WHERE clause. You can also combine AND and OR. IN (value1,value2,..) The IN operator may be used if you know the exact value you want to return for at least one of the columns. BETWEEN lowest_value AND highest_value To select range of data. LIKE % zero or more _ one character

Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’ SELECT BDATE,ADDRESS FROM EMPLOYEE WHERE FNAME=‘John’ AND MINIT=‘B’ AND LNAME=‘Smith’;

Retrieve the name(s)of the employee(s) whose salary not equal 38000 SELECT fname,lname FROM EMPLOYEE WHERE salary <> 38000;

Retrieve all employees in department 5 whose salary is between $30,000 and $40,000. SELECT * FROM EMPLOYEE WHERE (SALARY BETWEEN 30000 AND 40000) AND DNO = 5;

Selecting Rows from a Set of Values Using IN Operator The IN operator tests for values in a specified list. For example, if you want to list employees who are working in either dno 1, or dno 2 or dno 4, then you may use the following SQL statement Select * From employee Where dno=1 OR dno = 2 OR dno = 4; The above query can be rewritten using IN operator as Select * Where DNO IN (1, 3, 4);

Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1, 2, 3)

Substring Comparison SQL allows comparison conditions on only parts of a character string , using LIKE comparison operator. Partial strings are specified by using two reserved character: % : replaces an arbitrary number of characters. _ : replaces a single character.

Example of substring comparisons 'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would both satisfy the condition. '%XYZ': All strings that end with 'XYZ'. For example, 'WXYZ' and 'ZZXYZ' would both satisfy the condition. '%AN%': All strings that contain the pattern 'AN' anywhere. For example, 'LOS ANGELES' and 'SAN FRANCISCO' would both satisfy the condition. ‘C _ _‘ : Any three characters long string that starts with C . For example , ‘Car’ and ‘Cat’ would both satisfy the condition.

Retrieve all employees whose address is Huston, Texas SELECT FNAME, LNAME FROM EMPLOYEE WHERE ADDRESS LIKE '%Houston,TX%';

Retrieve all employees who were born during the 1950s. SELECT FNAME, LNAME FROM EMPLOYEE WHERE BDATE LIKE ’_ _ _ _ _ _ _ _ _5_';

NULLS IN SQL QUERIES SQL allows queries that check if a value is NULL (missing or undefined or not applicable) SQL uses IS or IS NOT to compare NULLs because it considers each NULL value distinct from other NULL values, so equality comparison is not appropriate . Retrieve the names of all employees who do not have supervisors. SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the result