Pernyataan Pilih SQL

Sing SELECT pratelan ing SQL digunakake kanggo njupuk data saka tabel database.

Kita bisa nggunakake SELECT pratelan kanggo entuk data saka kolom sing ditemtokake utawa kabeh kolom.



SQL PILIH Sintaks

Sing SELECT pratelan duwe sintaks ing ngisor iki:


SELECT column1, column2, ... FROM table_name;

Tuladha Statistik PILIH SQL

Umpama kita duwe tabel sing diarani 'Karyawan' kanthi data ing ngisor iki:

+------------+-----------+----------+------------+ | EmployeeID | FirstName | LastName | Department | +------------+-----------+----------+------------+ | 1

| Mark
| Otto
| Finance | | 2

| Jacob
| Thornton | IT
| | 3

| Su
| Bird
| Marketing | | 4

| Sam
| Burger | IT
| +------------+-----------+----------+------------+


PILIH Saka Kolom

Kanggo milih kolom 'Departemen' saka tabel 'Karyawan':


SELECT Department FROM Employees;

Output:



+------------+ | Department | +------------+ | Finance | | IT
| | Marketing | | IT
| +------------+


PILIH Saka Multiple Columns

Kanggo milih kolom 'Departemen' lan 'Karyawan' saka tabel 'Karyawan':

SELECT EmployeeID, Department FROM Employees;

Output:

+------------+------------+ | EmployeeID | Department | +------------+------------+ | 1

| Finance | | 2

| IT
| | 3

| Marketing | | 4

| IT
| +------------+------------+


PILIH * Pratelan

Kanggo milih kabeh kolom saka tabel 'Karyawan':


SELECT * FROM Employees;

Output:

+------------+-----------+----------+------------+ | EmployeeID | FirstName | LastName | Department | +------------+-----------+----------+------------+ | 1

| Mark
| Otto
| Finance | | 2

| Jacob
| Thornton | IT
| | 3

| Su
| Bird
| Marketing | | 4

| Sam
| Burger | IT
| +------------+-----------+----------+------------+


Hasil Nyaring Nggunakake Klausa WHERE

Kita bisa nyaring asil saka SELECT pitakon kanthi nggunakake WHERE klausa

Contone, kanggo milih kabeh karyawan ing 'Departemen TI' sing digunakake:

SELECT * FROM Employees WHERE Department='IT'

Output:


+------------+-----------+----------+------------+ | EmployeeID | FirstName | LastName | Department | +------------+-----------+----------+------------+ | 2

| Jacob
| Thornton | IT
| | 4

| Sam
| Burger | IT
| +------------+-----------+----------+------------+

Kajaba iku, kanggo milih kabeh karyawan Ids sing ana ing 'Departemen TI' sing digunakake:

SELECT EmployeeID FROM Employees WHERE Department='IT'

Output:

+------------+ | EmployeeID | +------------+ | 2

| | 4

| +------------+