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.
Ad

More Related Content

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

DP080_Lecture_1 SQL lecture document .pdf
DP080_Lecture_1 SQL lecture document .pdfDP080_Lecture_1 SQL lecture document .pdf
DP080_Lecture_1 SQL lecture document .pdf
MinhTran394436
 
intro for sql
intro for sql intro for sql
intro for sql
mahmoud mones
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
AmitDas125851
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
Md. Mahedee Hasan
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
PavithSingh
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql
MengChun Lam
 
Unit4_Lecture-sql.ppt and data science relate
Unit4_Lecture-sql.ppt and data science relateUnit4_Lecture-sql.ppt and data science relate
Unit4_Lecture-sql.ppt and data science relate
umang2782love
 
Unit - II.pptx
Unit - II.pptxUnit - II.pptx
Unit - II.pptx
MrsSavitaKumbhare
 
Lecture 2 sql {basics date type, constrains , integrity types etc.}
Lecture 2 sql {basics  date type, constrains , integrity types etc.}Lecture 2 sql {basics  date type, constrains , integrity types etc.}
Lecture 2 sql {basics date type, constrains , integrity types etc.}
Shubham Shukla
 
SQL
SQLSQL
SQL
Devyani Chaudhari
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
TamiratDejene1
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptx
Sheethal Aji Mani
 
Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)
Rakibul Hasan Pranto
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune SlidesSQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slides
enosislearningcom
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*Plus
Chhom Karath
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server Databases
ColdFusionConference
 
Module02
Module02Module02
Module02
Sridhar P
 
Oracle Course
Oracle CourseOracle Course
Oracle Course
rspaike
 
sql-commands.pdf
sql-commands.pdfsql-commands.pdf
sql-commands.pdf
Prof. Dr. K. Adisesha
 
Sql commands
Sql commandsSql commands
Sql commands
Prof. Dr. K. Adisesha
 
DP080_Lecture_1 SQL lecture document .pdf
DP080_Lecture_1 SQL lecture document .pdfDP080_Lecture_1 SQL lecture document .pdf
DP080_Lecture_1 SQL lecture document .pdf
MinhTran394436
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
PavithSingh
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql
MengChun Lam
 
Unit4_Lecture-sql.ppt and data science relate
Unit4_Lecture-sql.ppt and data science relateUnit4_Lecture-sql.ppt and data science relate
Unit4_Lecture-sql.ppt and data science relate
umang2782love
 
Lecture 2 sql {basics date type, constrains , integrity types etc.}
Lecture 2 sql {basics  date type, constrains , integrity types etc.}Lecture 2 sql {basics  date type, constrains , integrity types etc.}
Lecture 2 sql {basics date type, constrains , integrity types etc.}
Shubham Shukla
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
TamiratDejene1
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptx
Sheethal Aji Mani
 
Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)
Rakibul Hasan Pranto
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune SlidesSQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slides
enosislearningcom
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*Plus
Chhom Karath
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server Databases
ColdFusionConference
 
Oracle Course
Oracle CourseOracle Course
Oracle Course
rspaike
 

Recently uploaded (20)

Secure_File_Storage_Hybrid_Cryptography.pptx..
Secure_File_Storage_Hybrid_Cryptography.pptx..Secure_File_Storage_Hybrid_Cryptography.pptx..
Secure_File_Storage_Hybrid_Cryptography.pptx..
yuvarajreddy2002
 
Call illuminati Agent in uganda+256776963507/0741506136
Call illuminati Agent in uganda+256776963507/0741506136Call illuminati Agent in uganda+256776963507/0741506136
Call illuminati Agent in uganda+256776963507/0741506136
illuminati Agent uganda call+256776963507/0741506136
 
...lab.Lab123456789123456789123456789123456789
...lab.Lab123456789123456789123456789123456789...lab.Lab123456789123456789123456789123456789
...lab.Lab123456789123456789123456789123456789
Ghh
 
Process Mining at AE - Key success factors
Process Mining at AE - Key success factorsProcess Mining at AE - Key success factors
Process Mining at AE - Key success factors
Process mining Evangelist
 
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
Taqyea
 
Process Mining and Official Statistics - CBS
Process Mining and Official Statistics - CBSProcess Mining and Official Statistics - CBS
Process Mining and Official Statistics - CBS
Process mining Evangelist
 
定制(意大利Rimini毕业证)布鲁诺马代尔纳嘉雷迪米音乐学院学历认证
定制(意大利Rimini毕业证)布鲁诺马代尔纳嘉雷迪米音乐学院学历认证定制(意大利Rimini毕业证)布鲁诺马代尔纳嘉雷迪米音乐学院学历认证
定制(意大利Rimini毕业证)布鲁诺马代尔纳嘉雷迪米音乐学院学历认证
Taqyea
 
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
James Francis Paradigm Asset Management
 
Minions Want to eat presentacion muy linda
Minions Want to eat presentacion muy lindaMinions Want to eat presentacion muy linda
Minions Want to eat presentacion muy linda
CarlaAndradesSoler1
 
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
OlhaTatokhina1
 
Process Mining and Data Science in the Financial Industry
Process Mining and Data Science in the Financial IndustryProcess Mining and Data Science in the Financial Industry
Process Mining and Data Science in the Financial Industry
Process mining Evangelist
 
MASAkkjjkttuyrdquesjhjhjfc44dddtions.docx
MASAkkjjkttuyrdquesjhjhjfc44dddtions.docxMASAkkjjkttuyrdquesjhjhjfc44dddtions.docx
MASAkkjjkttuyrdquesjhjhjfc44dddtions.docx
santosh162
 
Principles of information security Chapter 5.ppt
Principles of information security Chapter 5.pptPrinciples of information security Chapter 5.ppt
Principles of information security Chapter 5.ppt
EstherBaguma
 
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
Taqyea
 
E-Book-TOEFL-Masuk-PTN.pdf hahahahaahahahah
E-Book-TOEFL-Masuk-PTN.pdf hahahahaahahahahE-Book-TOEFL-Masuk-PTN.pdf hahahahaahahahah
E-Book-TOEFL-Masuk-PTN.pdf hahahahaahahahah
RyanRahardjo2
 
717239550-Hotel-Management-Ppt-Final.pptx
717239550-Hotel-Management-Ppt-Final.pptx717239550-Hotel-Management-Ppt-Final.pptx
717239550-Hotel-Management-Ppt-Final.pptx
dharmendrasingh31102
 
Chapter-3-PROBLEM-SOLVING.pdf hhhhhhhhhh
Chapter-3-PROBLEM-SOLVING.pdf hhhhhhhhhhChapter-3-PROBLEM-SOLVING.pdf hhhhhhhhhh
Chapter-3-PROBLEM-SOLVING.pdf hhhhhhhhhh
ChrisjohnAlfiler
 
L1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptxL1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptx
38NoopurPatel
 
IAS-slides2-ia-aaaaaaaaaaain-business.pdf
IAS-slides2-ia-aaaaaaaaaaain-business.pdfIAS-slides2-ia-aaaaaaaaaaain-business.pdf
IAS-slides2-ia-aaaaaaaaaaain-business.pdf
mcgardenlevi9
 
Customer Segmentation using K-Means clustering
Customer Segmentation using K-Means clusteringCustomer Segmentation using K-Means clustering
Customer Segmentation using K-Means clustering
Ingrid Nyakerario
 
Secure_File_Storage_Hybrid_Cryptography.pptx..
Secure_File_Storage_Hybrid_Cryptography.pptx..Secure_File_Storage_Hybrid_Cryptography.pptx..
Secure_File_Storage_Hybrid_Cryptography.pptx..
yuvarajreddy2002
 
...lab.Lab123456789123456789123456789123456789
...lab.Lab123456789123456789123456789123456789...lab.Lab123456789123456789123456789123456789
...lab.Lab123456789123456789123456789123456789
Ghh
 
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
Taqyea
 
Process Mining and Official Statistics - CBS
Process Mining and Official Statistics - CBSProcess Mining and Official Statistics - CBS
Process Mining and Official Statistics - CBS
Process mining Evangelist
 
定制(意大利Rimini毕业证)布鲁诺马代尔纳嘉雷迪米音乐学院学历认证
定制(意大利Rimini毕业证)布鲁诺马代尔纳嘉雷迪米音乐学院学历认证定制(意大利Rimini毕业证)布鲁诺马代尔纳嘉雷迪米音乐学院学历认证
定制(意大利Rimini毕业证)布鲁诺马代尔纳嘉雷迪米音乐学院学历认证
Taqyea
 
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
Safety Innovation in Mt. Vernon A Westchester County Model for New Rochelle a...
James Francis Paradigm Asset Management
 
Minions Want to eat presentacion muy linda
Minions Want to eat presentacion muy lindaMinions Want to eat presentacion muy linda
Minions Want to eat presentacion muy linda
CarlaAndradesSoler1
 
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
2024-Media-Literacy-Index-Of-Ukrainians-ENG-SHORT.pdf
OlhaTatokhina1
 
Process Mining and Data Science in the Financial Industry
Process Mining and Data Science in the Financial IndustryProcess Mining and Data Science in the Financial Industry
Process Mining and Data Science in the Financial Industry
Process mining Evangelist
 
MASAkkjjkttuyrdquesjhjhjfc44dddtions.docx
MASAkkjjkttuyrdquesjhjhjfc44dddtions.docxMASAkkjjkttuyrdquesjhjhjfc44dddtions.docx
MASAkkjjkttuyrdquesjhjhjfc44dddtions.docx
santosh162
 
Principles of information security Chapter 5.ppt
Principles of information security Chapter 5.pptPrinciples of information security Chapter 5.ppt
Principles of information security Chapter 5.ppt
EstherBaguma
 
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
Taqyea
 
E-Book-TOEFL-Masuk-PTN.pdf hahahahaahahahah
E-Book-TOEFL-Masuk-PTN.pdf hahahahaahahahahE-Book-TOEFL-Masuk-PTN.pdf hahahahaahahahah
E-Book-TOEFL-Masuk-PTN.pdf hahahahaahahahah
RyanRahardjo2
 
717239550-Hotel-Management-Ppt-Final.pptx
717239550-Hotel-Management-Ppt-Final.pptx717239550-Hotel-Management-Ppt-Final.pptx
717239550-Hotel-Management-Ppt-Final.pptx
dharmendrasingh31102
 
Chapter-3-PROBLEM-SOLVING.pdf hhhhhhhhhh
Chapter-3-PROBLEM-SOLVING.pdf hhhhhhhhhhChapter-3-PROBLEM-SOLVING.pdf hhhhhhhhhh
Chapter-3-PROBLEM-SOLVING.pdf hhhhhhhhhh
ChrisjohnAlfiler
 
L1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptxL1_Slides_Foundational Concepts_508.pptx
L1_Slides_Foundational Concepts_508.pptx
38NoopurPatel
 
IAS-slides2-ia-aaaaaaaaaaain-business.pdf
IAS-slides2-ia-aaaaaaaaaaain-business.pdfIAS-slides2-ia-aaaaaaaaaaain-business.pdf
IAS-slides2-ia-aaaaaaaaaaain-business.pdf
mcgardenlevi9
 
Customer Segmentation using K-Means clustering
Customer Segmentation using K-Means clusteringCustomer Segmentation using K-Means clustering
Customer Segmentation using K-Means clustering
Ingrid Nyakerario
 
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.