.Dd Created:2026-03-23|Updated:2026-03-25| .de ocsi \\$* , .. .de oxr .ocsi .Xr \\$* .. .de oit .It \\$* .. .de obdi .Bl -dash -compact .oit \\$* .. .de obdl .Bd -literal -compact \\$* .. .de onote .Bl -hang -compact .oit \\$* .El .. .de ocomm .Bl -diag -compact .oit \\$* .El .. .de opsy .Pp .Sy - \\$* .. .de obc .Bl -column \\$* .. .de obc2 .obc opt desc .. .de obc3 .obc option arguments description .. .Dt SQL oh .Os linux , OpenBSD 7.8 | .Nm sql .Nd queries and usage. .Sh QUERIES .Bl -dash -compact .It select * will show all fields .It select distinct means it will only show unique records .El .Sh SELECT .obc2 .It Select all records from table Ta : Li select * from TABLE; .It Select unique records Ta : Li select distinct * from TABLE; .It Select all records, showing only specified fields Ta : Li select FIELD1, FIELD2 from TABLE; .It Count records from table Ta : Li select count(1) from TABLE; .It Select only records matching condition Ta : Li select * from TABLE where FIELD = VALUE; .El .Ss Multiple tables .obdi Select all record matching multiple conditions : .Dl select * from TABLE where FIELD1 = VALUE1 and FIELD2 = VALUE2; .oit Select all matching records from multiple tables : .Dl select * from TABLE1, TABLE2 where TABLE1.FIELD1 = TABLE2.FIELD2; .El .Ss alias .obdi Set table alias to simplify use: .Dl select * from TABLE1 ALIAS1; .oit Use the alias when selecting: .Dl select T1.FIELD1, T1.FIELD2 from TABLE1 T1; .El .Sh GROUPING .obdi Select record count by group: .Dl select FIELD, count(FIELD) from TABLE group by FIELD; .oit Select record count by group where count is greater than 1 : .Dl select FIELD, count(FIELD) from TABLE group by FIELD having count(FIELD) > 1; .El Grouping functions include: count, avg, min, max, sum. Example: .Li select FIELD1, sum(FIELD2) from TABLE group by FIELD1; .Sh LIMITS .obc2 .It Limit displayed records Ta : Li select * from TABLE limit 10; .It Select 10 records starting from the 5th record Ta : Li select * from TABLE limit 10 offset 5; .El .Sh JOINS Joins data from multiple tables using [INNER] JOIN. JOIN implies INNER JOIN. .obdl select T1.FIELD1, T1.FIELD2, T2.FIELD1, ALIAs2.FIELD2 from TABLE1 T1 join TABLE2 T2 on T2.FIELD = T1.FIELD; .Ed .Ss outer join Displays records matching any of the joined tables. Possible values are [LEFT|RIGHT|FULL] [OUTER] JOIN. Specifying OUTER is not necessary when using LEFT, RIGHT, FULL as it's implied. .Ss left Displays all records from the left table and matching records from the right table. Right is similar only showing records from right and matching from left. .obdl select * from TABLE1 T1 right join TABLE2 T2 on T2.FIELD = T1.FIELD; .Ed .Ss full Shows all records from both tables. .Sh SEE ALSO .oxr sqlite3 oh .Xr mysql oh .Sh AUTHORS .An -nosplit .Xr ohazot oh | .Xr about oh | .Lk https://ohazot.com ohazot.com .Aq Mt admin@ohazot.com