SlideShare a Scribd company logo
The SQL Query
Language: Simple
SELECT Commands
Database Systems
9/19/24
Announcements
• Team Project
• Mid-terms
• Chapter review questions
• Question from students
• W3schools SQL Quiz
What is SQL?
• SQL stands for Structured Query Language and is a computer language that
we use to interact with a relational database.
• In a DBMS, the SQL database language is used to:
• Create the database and table structures
• Perform basic data management chores (add, delete and modify)
• Perform complex queries to transform raw data into useful information
• There are two types of SQL: data definition language (DDL) and data
manipulation language (DML)
• DDL- create the database and table structures
• DML- insert, delete, select and update data within the database tables
DDL
• The major SQL DDL statements are CREATE DATABASE and
CREATE/DROP/ALTER TABLE. The SQL statement CREATE is
used to create the database and table structures.
• CREATE: use the CREATE command to create a UNIVERSITY
database.
• CREATE DATABASE PetShop
CREATE TABLE command
• The general format for the CREATE TABLE command is:
• CREATE TABLE <tablename>
(
ColumnName, Datatype, Optional Column Constraint,
ColumnName, Datatype, Optional Column Constraint,
Optional table Constraints
);
• Optional column constraints include:
• NULL, NOT NULL, UNIQUE, PRIMARY KEY and DEFAULT
Data Type
• The data type must be a system data type or a user-defined data
type. Many of the data types have a size such as CHAR(35) or
Numeric(8,2).
• Bit –Integer data with either a 1 or 0 value
• Int –Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 – 1
(2,147,483,647)
• Smallint –Integer data from 2^15 (-32,768) through 2^15 – 1 (32,767)
• Tinyint –Integer data from 0 through 255
• Decimal –Fixed precision and scale numeric data from -10^38 -1 through 10^38
• Numeric –A synonym for decimal
• Timestamp –A database-wide unique number
• Uniqueidentifier –A globally unique identifier (GUID)
• Money – Monetary data values from -2^63 (-922,337,203,685,477.5808) through
2^63 – 1 (+922,337,203,685,477.5807), with accuracy to one-ten-thousandth of a
monetary unit
• Smallmoney –Monetary data values from -214,748.3648 through
+214,748.3647, with accuracy to one-ten-thousandth of a monetary unit
Data Types continued
• Float –Floating precision number data from -1.79E + 308 through 1.79E + 308
• Real –Floating precision number data from -3.40E + 38 through 3.40E + 38
• Datetime –Date and time data from January 1, 1753, to December 31, 9999,
with an accuracy of one-three-hundredths of a second, or 3.33 milliseconds
• Smalldatetime –Date and time data from January 1, 1900, through June 6,
2079, with an accuracy of one minute
• Char –Fixed-length non-Unicode character data with a maximum length of
8,000 characters
• Varchar –Variable-length non-Unicode data with a maximum of 8,000
characters
• Text –Variable-length non-Unicode data with a maximum length of 2^31 – 1
(2,147,483,647) characters
• Binary –Fixed-length binary data with a maximum length of 8,000 bytes
• Varbinary –Variable-length binary data with a maximum length of 8,000 bytes
• Image – Variable-length binary data with a maximum length of 2^31 – 1
(2,147,483,647) bytes
Primary Key if Composite and Foreign
Key
USE HOTEL
GO
CREATE TABLE tblRoom
(
HotelNo Int NOT NULL ,
RoomNo Int NOT NULL,
Type Char(50) NULL,
Price Money NULL,
PRIMARY KEY (HotelNo, RoomNo),
FOREIGN KEY (HotelNo) REFERENCES tblHotel
)
The SQL Query Language: Simple SELECT Commands
DML
• The SQL data manipulation language (DML) is used to query
and modify database data. In this chapter, we will describe
how to use the SELECT, INSERT, UPDATE, and DELETE SQL
DML command statements, defined below.
• SELECT – to query data in the database
• INSERT – to insert data into a table
• UPDATE – to update data in a table
• DELETE – to delete data from a table
In the SQL DML statement:
• Each clause in a statement should begin on a new line.
• The beginning of each clause should line up with the
beginning of other clauses.
• If a clause has several parts, they should appear on separate
lines and be indented under the start of the clause to show
the relationship.
• Upper case letters are used to represent reserved words.
• Lower case letters are used to represent user-defined
words.
The SQL Query Language: Simple SELECT Commands
SELECT (from a single table)
• Sample query:
SELECT student_id
FROM Enrolled
WHERE credit_status = 'grad’;
• Basic syntax:
SELECT column1, column2, …
FROM table
WHERE selection condition;
• the FROM clause specifies which table you are using
• the WHERE clause specifies which rows should be included in the result
• the SELECT clause specifies which columns should be included
SELECT (from a single table) (cont.)
• Example:
SELECT student_id
FROM Enrolled
WHERE credit_status = 'grad';
Selecting Entire Column
• If there's no WHERE clause, the result will consist of one or
more entire columns. No rows will be excluded.
SELECT student_id
FROM Enrolled;
Selecting Entire Rows
• If we want the result to include entire rows (i.e., all of the
columns), we use a * in the SELECT clause:
SELECT *
FROM Enrolled
WHERE credit_status = 'grad';
The WHERE Clause
• The selection condition must be an expression that
evaluates to either true or false.
• example: credit_status = 'grad’
• can include any column from the table(s) in the FROM clause
• The results of the SELECT command will include only those
tuples for which the selection condition evaluates to true.
Simple Comparisons
• The simplest selection condition is a comparison that uses
one of the following comparison operators:
Practice
• Write a query that finds the names and capacities of all
rooms that hold at least 70 people.
Practice
• Write a query that finds the names and capacities of all
rooms that hold at least 70 people.
The SQL Query
Language: Simple
SELECT Commands
Why Learn SQL?
• Desktop database systems like Access provide tools for
manipulating data in a database.
• However, these tools don't allow you to perform all possible
types of queries.
• For more flexibility and power, we use SQL.
• a query language
• In addition, knowledge of SQL is needed to perform queries
from within a program.
How could we get all info about movies
released in 2010?
How could we get all info about movies
released before 2010?
How could we get the name and
runtime of movies released before
2010?
How could we get the name and
runtime of all movies?
Forming More Complex Selection
Conditions
• We often want to combine conditions or take the opposite
of one.
• SQL provides logical operators for this purpose:
Range Comparisons
• SQL also provides a special operator called BETWEEN for
checking if a value falls within a range of values.
How could we get the name and
runtime of both Titanic and Toy Story 3?
The SQL Query Language: Simple SELECT Commands
Practice with Simple SQL Queries
• Write a query that finds all information about CAS 315.
• Write a query that lists the names and start times of all
courses.
• Write a query that gets the ID numbers of student(s) who
are taking CS 105 for undergraduate (ugrad) credit.

More Related Content

PPTX
SQL Commands
PPTX
MYSQL Presentation for SQL database connectivity
PDF
IR SQLite Session #1
PPTX
HPD SQL Training - Beginner - 20220916.pptx
PPTX
Using Basic Structured Query Language lo1.pptx
PPTX
How Clean is your Database? Data Scrubbing for all Skill Sets
PDF
Sql tutorial
PDF
Sql tutorial
SQL Commands
MYSQL Presentation for SQL database connectivity
IR SQLite Session #1
HPD SQL Training - Beginner - 20220916.pptx
Using Basic Structured Query Language lo1.pptx
How Clean is your Database? Data Scrubbing for all Skill Sets
Sql tutorial
Sql tutorial

Similar to The SQL Query Language: Simple SELECT Commands (20)

PPTX
Lecture - MY-SQL/ SQL Commands - DDL.pptx
PDF
DP080_Lecture_1 SQL lecture document .pdf
PPTX
intro for sql
PPTX
SQL.pptx
PPSX
MS SQL Server
PPTX
SQL.pptx for the begineers and good know
PPTX
Tk2323 lecture 7 sql
PPT
Unit4_Lecture-sql.ppt and data science relate
PPTX
Unit - II.pptx
PPTX
Lecture 2 sql {basics date type, constrains , integrity types etc.}
PDF
Chapter – 6 SQL Lab Tutorial.pdf
PPTX
Unit 10 - Realtional Databases.pptxxxxxxxxx
PPTX
Relational Database Language.pptx
PPTX
Islamic University Previous Year Question Solution 2018 (ADBMS)
PPTX
SQL SERVER Training in Pune Slides
PPTX
Data Query Using Structured Query Language - WITH NOTES.pptx
PPTX
shs tvl ict_Programming Introduction to SQl.pptx
PPTX
Introduction to SQL, SQL*Plus
PDF
Building better SQL Server Databases
Lecture - MY-SQL/ SQL Commands - DDL.pptx
DP080_Lecture_1 SQL lecture document .pdf
intro for sql
SQL.pptx
MS SQL Server
SQL.pptx for the begineers and good know
Tk2323 lecture 7 sql
Unit4_Lecture-sql.ppt and data science relate
Unit - II.pptx
Lecture 2 sql {basics date type, constrains , integrity types etc.}
Chapter – 6 SQL Lab Tutorial.pdf
Unit 10 - Realtional Databases.pptxxxxxxxxx
Relational Database Language.pptx
Islamic University Previous Year Question Solution 2018 (ADBMS)
SQL SERVER Training in Pune Slides
Data Query Using Structured Query Language - WITH NOTES.pptx
shs tvl ict_Programming Introduction to SQl.pptx
Introduction to SQL, SQL*Plus
Building better SQL Server Databases
Ad

Recently uploaded (20)

PPT
Quality review (1)_presentation of this 21
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
PPTX
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
PPTX
Global journeys: estimating international migration
PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Quality review (1)_presentation of this 21
Business Acumen Training GuidePresentation.pptx
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
Global journeys: estimating international migration
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
oil_refinery_comprehensive_20250804084928 (1).pptx
Business Ppt On Nestle.pptx huunnnhhgfvu
Introduction-to-Cloud-ComputingFinal.pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
climate analysis of Dhaka ,Banglades.pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
.pdf is not working space design for the following data for the following dat...
Acceptance and paychological effects of mandatory extra coach I classes.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Ad

The SQL Query Language: Simple SELECT Commands

  • 1. The SQL Query Language: Simple SELECT Commands Database Systems 9/19/24
  • 2. Announcements • Team Project • Mid-terms • Chapter review questions • Question from students • W3schools SQL Quiz
  • 3. What is SQL? • SQL stands for Structured Query Language and is a computer language that we use to interact with a relational database. • In a DBMS, the SQL database language is used to: • Create the database and table structures • Perform basic data management chores (add, delete and modify) • Perform complex queries to transform raw data into useful information • There are two types of SQL: data definition language (DDL) and data manipulation language (DML) • DDL- create the database and table structures • DML- insert, delete, select and update data within the database tables
  • 4. DDL • The major SQL DDL statements are CREATE DATABASE and CREATE/DROP/ALTER TABLE. The SQL statement CREATE is used to create the database and table structures. • CREATE: use the CREATE command to create a UNIVERSITY database. • CREATE DATABASE PetShop
  • 5. CREATE TABLE command • The general format for the CREATE TABLE command is: • CREATE TABLE <tablename> ( ColumnName, Datatype, Optional Column Constraint, ColumnName, Datatype, Optional Column Constraint, Optional table Constraints ); • Optional column constraints include: • NULL, NOT NULL, UNIQUE, PRIMARY KEY and DEFAULT
  • 6. Data Type • The data type must be a system data type or a user-defined data type. Many of the data types have a size such as CHAR(35) or Numeric(8,2). • Bit –Integer data with either a 1 or 0 value • Int –Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 – 1 (2,147,483,647) • Smallint –Integer data from 2^15 (-32,768) through 2^15 – 1 (32,767) • Tinyint –Integer data from 0 through 255 • Decimal –Fixed precision and scale numeric data from -10^38 -1 through 10^38 • Numeric –A synonym for decimal • Timestamp –A database-wide unique number • Uniqueidentifier –A globally unique identifier (GUID) • Money – Monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 – 1 (+922,337,203,685,477.5807), with accuracy to one-ten-thousandth of a monetary unit • Smallmoney –Monetary data values from -214,748.3648 through +214,748.3647, with accuracy to one-ten-thousandth of a monetary unit
  • 7. Data Types continued • Float –Floating precision number data from -1.79E + 308 through 1.79E + 308 • Real –Floating precision number data from -3.40E + 38 through 3.40E + 38 • Datetime –Date and time data from January 1, 1753, to December 31, 9999, with an accuracy of one-three-hundredths of a second, or 3.33 milliseconds • Smalldatetime –Date and time data from January 1, 1900, through June 6, 2079, with an accuracy of one minute • Char –Fixed-length non-Unicode character data with a maximum length of 8,000 characters • Varchar –Variable-length non-Unicode data with a maximum of 8,000 characters • Text –Variable-length non-Unicode data with a maximum length of 2^31 – 1 (2,147,483,647) characters • Binary –Fixed-length binary data with a maximum length of 8,000 bytes • Varbinary –Variable-length binary data with a maximum length of 8,000 bytes • Image – Variable-length binary data with a maximum length of 2^31 – 1 (2,147,483,647) bytes
  • 8. Primary Key if Composite and Foreign Key USE HOTEL GO CREATE TABLE tblRoom ( HotelNo Int NOT NULL , RoomNo Int NOT NULL, Type Char(50) NULL, Price Money NULL, PRIMARY KEY (HotelNo, RoomNo), FOREIGN KEY (HotelNo) REFERENCES tblHotel )
  • 10. DML • The SQL data manipulation language (DML) is used to query and modify database data. In this chapter, we will describe how to use the SELECT, INSERT, UPDATE, and DELETE SQL DML command statements, defined below. • SELECT – to query data in the database • INSERT – to insert data into a table • UPDATE – to update data in a table • DELETE – to delete data from a table
  • 11. In the SQL DML statement: • Each clause in a statement should begin on a new line. • The beginning of each clause should line up with the beginning of other clauses. • If a clause has several parts, they should appear on separate lines and be indented under the start of the clause to show the relationship. • Upper case letters are used to represent reserved words. • Lower case letters are used to represent user-defined words.
  • 13. SELECT (from a single table) • Sample query: SELECT student_id FROM Enrolled WHERE credit_status = 'grad’; • Basic syntax: SELECT column1, column2, … FROM table WHERE selection condition; • the FROM clause specifies which table you are using • the WHERE clause specifies which rows should be included in the result • the SELECT clause specifies which columns should be included
  • 14. SELECT (from a single table) (cont.) • Example: SELECT student_id FROM Enrolled WHERE credit_status = 'grad';
  • 15. Selecting Entire Column • If there's no WHERE clause, the result will consist of one or more entire columns. No rows will be excluded. SELECT student_id FROM Enrolled;
  • 16. Selecting Entire Rows • If we want the result to include entire rows (i.e., all of the columns), we use a * in the SELECT clause: SELECT * FROM Enrolled WHERE credit_status = 'grad';
  • 17. The WHERE Clause • The selection condition must be an expression that evaluates to either true or false. • example: credit_status = 'grad’ • can include any column from the table(s) in the FROM clause • The results of the SELECT command will include only those tuples for which the selection condition evaluates to true.
  • 18. Simple Comparisons • The simplest selection condition is a comparison that uses one of the following comparison operators:
  • 19. Practice • Write a query that finds the names and capacities of all rooms that hold at least 70 people.
  • 20. Practice • Write a query that finds the names and capacities of all rooms that hold at least 70 people.
  • 21. The SQL Query Language: Simple SELECT Commands
  • 22. Why Learn SQL? • Desktop database systems like Access provide tools for manipulating data in a database. • However, these tools don't allow you to perform all possible types of queries. • For more flexibility and power, we use SQL. • a query language • In addition, knowledge of SQL is needed to perform queries from within a program.
  • 23. How could we get all info about movies released in 2010?
  • 24. How could we get all info about movies released before 2010?
  • 25. How could we get the name and runtime of movies released before 2010?
  • 26. How could we get the name and runtime of all movies?
  • 27. Forming More Complex Selection Conditions • We often want to combine conditions or take the opposite of one. • SQL provides logical operators for this purpose:
  • 28. Range Comparisons • SQL also provides a special operator called BETWEEN for checking if a value falls within a range of values.
  • 29. How could we get the name and runtime of both Titanic and Toy Story 3?
  • 31. Practice with Simple SQL Queries • Write a query that finds all information about CAS 315. • Write a query that lists the names and start times of all courses. • Write a query that gets the ID numbers of student(s) who are taking CS 105 for undergraduate (ugrad) credit.